Tip

Multithreading with 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. This multithreading can help programmers create more user friendly applications. This article from InformIT

    Requires Free Membership to View

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.
    ((AutoResetEvent)state).Set();
}

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

AutoResetEvent IsDone = new 
AutoResetEvent(false);
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 first published in July 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
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
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.