Browse DevX
Sign up for e-mail newsletters from DevX


Get Started with Multithreading in .NET  : Page 2

Have you ever built an application where users had to wait while the application performed some lengthy calculation or operation? Learn how to improve your application's responsiveness by creating and controlling threads.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The .NET Framework provides two methods to implementing a new thread in your program. The first, which is easier to use but does not provide as much control of the thread, makes use of the thread pool. The thread pool is a queue of idle threads managed by the .NET runtime . To assign some code to a new thread, your program sends a request to the thread pool. The pool manager assigns an available thread to the code, executes it, and then returns the thread to the pool when the code completes. To add a thread to the thread pool, you call the static ThreadPool.QueueUserWorkItem method. Like all threading-related classes, the ThreadPool class is in the System.Threading namespace. The syntax for this method is:

ThreadPool.QueueUserWorkItem( new WaitCallback(ThreadCode), Done);

The ThreadCode argument is the name of the method in your program where the new thread should start executing. The Done argument is an AutoResetEvent object that you use to signal the thread pool manager that you're done with the thread. The method specified by the ThreadCode argument must meet the following two requirements:

  1. Its signature must match that of the WaitCallback class. Specifically, it must take one argument of type Object, and must have a void return value (in VB.NET, use a Sub method rather than a Function).
  2. When the thread code completes it must inform the thread pool that it is finished.
The following example shows a method that meets the two requirements for starting thread execution:

static void MyThreadOperation(object state) { //Code to be executed by the thread goes here, including //calls to other methods as required. //Signal that the thread has finished executing. ((AutoResetEvent)state).Set(); }

You would start this thread running with the following code:

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

A Demonstration
The following short program demonstrates how to create and use a thread through the thread pool, and also illustrates how multithreading can improve a program's responsiveness. The plan is to create a time-consuming process that can be run two ways—as a separate thread or as part of the program's default thread. This process mimics complex calculations or I/O for which a real-world program might use multithreading. You can elect to run the time-consuming process on a single thread or in its own thread. While the long process is running you can still edit the text in a text box control to experiment with the difference in the program's responsiveness to user input when you add the second thread. Figure 2 shows the sample application's interface.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date