Multithreading with the .NET Framework

Using multithreading to improve user experience.

When your computer appears to be doing multiple things at once, what is really happening is that the CPU's time...

is being divided up between the various tasks. This multithreading can help programmers create more user friendly applications. This article from InformIT discusses reasons for multithreading and how to do it within the .NET Framework.

When your computer appears to be doing multiple things at once, what is really happening is that the CPU's time is being divided up between the various tasks. Task 1 -- printing, for example -- gets the CPU for some fraction of a second, then task 2, task 3, and so on. Because the computer is very fast and human perceptions are relatively slow it seems as if the tasks are being done simultaneously. This ability to share the CPU among multiple tasks is called multitasking, and it is a feature of the Windows operating system (and most other operating systems too).

How does threading relate to the .NET programmer? A .NET program by default has a single thread, but you can create two or more threads within a single program if you desire. Because these threads are all part of the same program, or process, they all have access to the program's resources (global, static, and instance fields, for example). But because Windows allocates CPU time based on threads, not processes, a multithreaded program will get more than its normal share of CPU time.

The .NET Framework offers two ways to create a new thread in your program. This article explains the simpler of these two methods, the thread pool, which is perfectly adequate for many uses. The .NET runtime maintains a queue of idle threads and assigns them to programs when requested. When the code that the program executes in the thread is complete, the thread is returned to the pool. This is why this method is so simple; the runtime takes care of most of the details for you via the ThreadPool class.

All ThreadPool threads have normal priority, which is what you want when you are using a second thread to improve program responsiveness. The ThreadPool class and all other multithreading-related classes are in the System. Threading namespace.

To create a new thread, call the static ThreadPool.QueueUserWorkItem method. The syntax is as follows:

ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadCode), Done);
  • ThreadCode is the name of the method in your program where the new thread should start and end executing.
  • Done is an AutoResetEvent object that you use to signal the thread pool manager that the program is finished using the thread.

The method specified by the ThreadCode argument must have the proper signature, matching that of the WaitCallback class. Specifically, it must take one argument of type Object and have a void return value. Code in this procedure must also inform the thread pool that it is finished. Here's an example shows a method that meets these two requirements:

static void MyThreadCode(object state)
    //Code to be executed by the thread goes here, including
    //calls to other methods as required. Execution must return
    //here when complete.
    //Tell the thread pool that the thread has finished.

You would create this thread and start it running as shown here:

AutoResetEvent IsDone = new 
ThreadPool.QueueUserWorkItem (new WaitCallback(MyThreadCode), IsDone);

Seems too simple to be true, right?

Click over to InformIT to read about the possible downsides of multithreading as well as to view a demonstration of the above method.

This was last published in July 2004

Dig Deeper on Microsoft .NET Web services



Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.