.NET Framework: Multithreading

This solution demonstrates how to use multiple threads to achieve concurrency in tasks. This example does not include synchronization, because no data is being accessed by multiple threads.

Featured Highlights

This application allows the user to fire a series of tasks on other threads. The top button runs the task on the same thread, effectively blocking the user from interacting with the main form until the task finishes. The second and third buttons run the task on other threads, allowing the user to continue interacting with the main form. The second button runs the task from a thread in the worker pool. The third button creates a new Win32 thread and runs the task on it.

Requirements

Requires the Trial or Release version of Visual Studio .NET Professional (or greater).

Running the Sample

This example does not include synchronization, as it is not needed for this code. However, when using multiple threads and sharing data, synchronization is critical. Consult the documentation for more information on synchronizing data in multithreaded applications.

Because multi-threaded code presents complexities when stepping through it, the DebuggerStepThrough attribute is applied at the multithreaded points. Attempting to step through the multi-threaded sections may yield inconsistent results. This example only writes to each window from the thread that is managing it. When writing to a window from threads other than the thread managing that window, additional attention is required. Consult the documentation for more details. An asynchronous delegate is an easy way to run a task on a worker thread, but is not the only way to do so. Consult the documentation to see other options for accessing the worker pool threads. 

Last Update: 7 July 2002