Threading enables your C# program to perform concurrent processing so that you can do more than one operation at a time. For example, you can use threading to monitor input from the user, perform background tasks, and handle simultaneous streams of input.
Threads have the following properties:
Threads enable your program to perform concurrent processing.
The .NET Framework System.Threading namespace makes using threads easier.
Threads share the application's resources. For more information, see Using Threads and Threading.
By default, a C# program has one thread. However, auxiliary threads can be created and used to execute code in parallel with the primary thread. These threads are often called worker threads.
Worker threads can be used to perform time-consuming or time-critical tasks without tying up the primary thread. For example, worker threads are often used in server applications to fulfill incoming requests without waiting for the previous request to be completed. Worker threads are also used to perform "background" tasks in desktop applications so that the main thread--which drives user interface elements--remains responsive to user actions.
Threading solves problems with throughput and responsiveness, but it can also introduce resource-sharing issues such as deadlocks and race conditions. Multiple threads are best for tasks that require different resources such as file handles and network connections. Assigning multiple threads to a single resource is likely to cause synchronization issues, and having threads frequently blocked when waiting for other threads defeats the purpose of using multiple threads.
A common strategy is to use worker threads to perform time-consuming or time-critical tasks that do not require many of the resources used by other threads. Naturally, some resources in your program must be accessed by multiple threads. For these cases, the System.Threading namespace provides classes for synchronizing threads. These classes include Mutex, Monitor, Interlocked, AutoResetEvent, and ManualResetEvent.
You can use some or all these classes to synchronize the activities of multiple threads, but some support for threading is supported by the C# language. For example, the Lock Statement provides synchronization features through implicit use of Monitor.
Beginning with the .NET Framework 4, multithreaded programming is greatly simplified with the System.Threading.Tasks.Parallel and System.Threading.Tasks.Task classes, Parallel LINQ (PLINQ), new concurrent collection classes in the System.Collections.Concurrent namespace, and a new programming model that is based on the concept of tasks rather than threads. For more information, see Parallel Programming.
|Multithreaded Applications (C#)||Describes how to create and use threads.|
|Thread Synchronization (C#)||Describes how to control the interactions of threads.|
|Thread Pooling (C#)||Describes how to use a pool of worker threads that are managed by the system.|
|How to: Use a Thread Pool (C#)||Demonstrates synchronized use of multiple threads in the thread pool.|
|Threading||Describes how to implement threading in the .NET Framework.|