System.Threading Namespace
Namespace
Namespace: System.Threading
Assembly: System.Runtime.dll (in .NET Framework 4.5 and later); mscorlib.dll (in .NET Framework versions prior to 4.5)
Provides classes and interfaces that enable you to create and manage threads, and that provide basic threading and synchronization primitives.
Classes
Name | Description | |
---|---|---|
C | CancellationToken | Represents an operation that can be cancelled. |
C | CancellationTokenRegistration | Represents a registration of a callback for a |
C | CancellationTokenSource | Encapsulates a |
C | Interlocked | Provides atomic operations that are used to access data that is shared by multiple threads. |
C | Lock | Provides a structure that is used to synchronize access to a code object. Obsolete. |
C | Monitor | Provides a way to enter and exit a synchronized block of code. |
C | ParameterizedThreadStart | Represents a method that can be executed by a new thread. |
C | SpinLock | Provides a lightweight synchronization primitive that uses spinning instead of blocking. |
C | SpinWait | Provides support for spin-waiting, which is a technique where a thread repeatedly checks for a condition without yielding the processor. |
C | Thread | Represents a thread of execution in the operating system. |
C | ThreadLocal | Provides thread-local storage. |
C | ThreadPool | Represents a collection of reusable threads that can be used to execute tasks. |
C | ThreadState | Specifies the execution states of a thread. |
C | ThreadStart | Represents a method that can be executed by a new thread. |
C | TimeoutException | The exception that is thrown when an asynchronous operation did not complete within the allotted time. |
Interfaces
Name | Description | |
---|---|---|
I | IAsyncResult | Specifies marker interface members that all asynchronous method classes must implement. |
I | ICancelableOperation | Represents an operation that can be cancelled. |
Enumerations
Name | Description | |
---|---|---|
E | LockRecursionPolicy | Specifies the recursion policy for a reentrant lock. |
E | ThreadPriority | Specifies the priority levels for threads. |
Example: Using ThreadPool
// Using System.Threading; public class ThreadPoolExample { public static void Main(string[] args) { for (int i = 0; i < 10; i++) { int taskId = i; ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessTask), taskId); } // Keep the console window open until a key is pressed. Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } static void ProcessTask(object state) { int taskId = (int)state; Console.WriteLine("Processing task #{0} on thread {1}", taskId, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(1000); // Simulate work Console.WriteLine("Finished task #{0}", taskId); } }