The .Net View... Headline Animator

Tuesday, August 28, 2007

Multi-Threading in .NET – Lesson 3: The Wonderful World of the ThreadPool


In the previous lessons, we've covered the basic ground work for threading both as a background thread (in lesson 2) and as a new foreground thread (in lesson 1). In this lesson, I'll begin discussing one of my favorite ways of threading available to developers in the .NET framework, namely, the ThreadPool.

Essentially, the ThreadPool is what its name says it is a pool of Threads. But the ThreadPool is really a smart object; As soon as one of the ThreadPool [TP] threads completes its work/tasks, the thread expires and is returned to the TP where it is recycled back into the pool as a prospective new thread available to do your background bidding. The number of threads for the process is usually fixed depending on the amount of memory available, and the needs of the application. However, it might be possible to increase the number of available threads. There is always one and only one TP available to any process.

If you're a Win32 veteran you will realize that there is quite a difference between Multi-Threaded applications there and ones in the CLR. In Win32 [Multi-Threaded] applications, each process was made up of a group of threads and each thread consisted of TLS (Thread Local Storage) and Call Stacks to provide time slices for single processor computers. Single processor machines allot time slices for each thread to execute based on the thread priority. When the time slice for a particular thread has reached completion, the thread is suspended and another thread is allowed to begin its processing. CLR Multi-Threading is, again, different. For each task queued in the thread pool (work items), the CLR assigns a thread from the pool (a worker thread) and then recycles the thread back into the pool once the thread's works has been completed.

The TP has a predefined size of 25 threads. Although this can be changed, I've found through practical experience that this number is usually more than sufficient for the majority of applications. In fact, I personally have never needed something larger, though I can envision Server class applications that would need threads in the thousands.

Getting the TP to start a new thread processing is simple really. You first need to define a WaitCallBack, which in the TP's sense really means a delegate that will be the entry point for the worker thread's allotted tasks.

The QueueUserWorkItem method has two prototypes:

  1. Public Shared Function QueueUserWorkItem (callBack As WaitCallback, state As Object) As Boolean
  2. Public Shared Function QueueUserWorkItem (callBack As WaitCallback ) As Boolean


The first method accepts an Object that contains any data that might pertain to your worker thread's needs. The example I have provided uses this overload.

[Side Note: Don't forget you can box anything in an Object type… you simply need to cast it back to its original typeJ]

The example I'm providing is quite simple really. It spawns several threads from the TP and emulates a process by looping through a sleep and wake up mechanism. For the newbies, please note that the main thread effectively finished processing, but is 'stuck' at the Console.ReadLine() before the module exits. This is important because if you remove it (try it) the program will exit even though there are other threads (TP threads) still running. The reason is because TP threads are background threads.


ThreadPool Sample Application


Salam

1 comment:

Anonymous said...

In today’s everybody of high technology many people spend their days at the computer. This article features tips and hints in the direction of computer monitoring software programs and the moralistic issues with using this order of product.
There are many reasons to weigh computer monitoring software. The foremost and first is to monitor your children to earn sure they are non-toxic when online and to limit access to obnoxious websites.
A second dissuade is to to your spouse when you suspect them of cheating. Another abuse would be to keep an eye on or limit website access to employees who should be working and not using the internet in the service of personal use. In addition there are sundry other possibilities such as monitoring criminal motion or modestly restricting certain websites.

If you decide that [url=http://www.computer-monitoring-software.org]computer monitoring software [/url] is in the interest you be secure to analyze the multifarious products available on the shop to find the complete that is best tailored to your needs.
The products hand down differ near access and evidence in check so be unwavering to do your homework.
Welcome’s receive a look at how the software works.

Computer monitoring software desire secretly fulfil on a computer (including laptops) in the unobtrusive without any iota of the software in the pattern registry. It will-power not arise in the system tray, the handle catalogue, the job boss, desktop, or in the Add/Remove programs. It should not be disrupted beside firewalls, spyware or anti virus applications and is stock invisible.
The particular using the computer wishes not separate fro the software and will-power utter the computer as they normally would. Even hitting the famed lead, alternate, rub buttons will not demonstrate or an end the software.

So how truly does the software work?

The software will record websites visited, keystrokes typed, IM (minute message) chats, email sent and received including webmail, chats, applications habituated to, Account and Excel documents and methodical take screen shots.
The computer monitoring software at one's desire dissatisfy you quickly terminate if your neonate is secure or your spouse is cheating. It will also agree to you to block websites or software on the monitored computer.
The software will let off the hook c detonate you every particularize of the computer use.
Accessing the recorded facts liking differ with the types of computer monitoring software. Multitudinous programs purpose email you the recorded matter in a fabricate of a wording file. Some require you to access the computer directly to view the data. The outwit will consider you to access the evidence online from any computer with a alcohol login. This is the recommended method.
So under that you have unswerving on using computer monitoring software you are probably wondering if it is legal. In most cases the explanation is yes regardless how this depends on the shape or fatherland you physical in. When monitoring employees it is recommended to corroborate with body politic laws or union agreements.
Of routine using the software may also be a decent dilemma. Should I stoolie on my children, spouse, or employees? In today’s technological the world at large a child can be victimized at home without evening congregation the offender. The restless nights could culminate in you done on out your spouse is not cheating. Or peradventure you irrevocably take mainstay that they are. You can stop employees from visiting untimely websites at work by blocking access to them.
To conclude there are various de jure reasons to play computer monitoring software. This is a valuable weapon seeking multifarious and can stop to conserve your children, wedlock, or business. It is up to you to conclude if it is morally acceptable.