Threading (C# and Visual Basic)

Threading enables your Visual Basic or 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 Visual Basic or 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 Visual Basic and C# languages. For example, the Visual Basic SyncLock Statement and the C# Lock Statement provide synchronization features through implicit use of Monitor.


In the .NET Framework 4, multithreaded programming is greatly simplified with the Parallel and 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 in the .NET Framework.



Multithreaded Applications (C# and Visual Basic)

Describes how to create and use threads.

Parameters and Return Values for Multithreaded Procedures (C# and Visual Basic)

Describes how to pass and return parameters with multithreaded applications.

Walkthrough: Multithreading with the BackgroundWorker Component (C# and Visual Basic)

Shows how to create a simple multithreaded application.

Thread Synchronization (C# and Visual Basic)

Describes how to control the interactions of threads.

Thread Timers (C# and Visual Basic)

Describes how to run procedures on separate threads at fixed intervals.

Thread Pooling (C# and Visual Basic)

Describes how to use a pool of worker threads that are managed by the system.

How to: Use a Thread Pool (C# and Visual Basic)

Demonstrates synchronized use of multiple threads in the thread pool.

Managed Threading

Describes how to implement threading in the .NET Framework.

Threading in C# 3.0 in a Nutshell, Third Edition: A Desktop Quick Reference

Asynchronous Methods in C# 3.0 in a Nutshell, Third Edition: A Desktop Quick Reference

Threading and Synchronization in C# 3.0 Cookbook, Third Edition: More than 250 solutions for C# 3.0 programmers