System.Threading Namespace

This namespace provides classes and interfaces that support the creation and management of threads. These classes allow you to manage the execution of code in parallel, synchronize threads, and handle thread-related exceptions.

Classes

C Thread

public sealed class Thread : CriticalFinalizerObject

Represents a thread, which is a single sequential flow of control in a process. The Thread class is used to create and manage threads.

Members:

C CancellationTokenSource

public sealed class CancellationTokenSource : IDisposable

Notifies operations that they should be canceled. This class is used to signal cancellation and to register callbacks that should be executed when cancellation is requested.

Members:

C ManualResetEvent

public class ManualResetEvent : WaitHandle

Signals one or more threads that an event has occurred. This class provides a simple way to synchronize threads.

Members:

C SemaphoreSlim

public class SemaphoreSlim : IDisposable

Represents a confining construct that limits the number of threads that can access a resource or perform an action concurrently. This is a lightweight semaphore, optimized for intra-process synchronization.

Members:

Interfaces

I IThreadLocal

public interface IThreadLocal

Represents a value that is local to a thread. This interface is used for implementing thread-local storage.

I ICancellationToken

public interface ICancellationToken

Represents a token that can be used to signal cancellation.

Enums

E ThreadState

public enum ThreadState

Specifies the execution state of a thread.

Values:

  • Running
  • Stopped
  • Suspended
  • Aborted
  • WaitSleepJoin
  • Suspended
  • Background
  • Unstarted
  • Stopped
  • Suspended
  • AbortRequested
  • StopRequested
  • SuspendRequested
  • ResumeRequested

Static Members

P Thread.CurrentThread

public static Thread CurrentThread { get; }

Gets the currently executing thread.

P Thread.ManagedThreadId

public int ManagedThreadId { get; }

Gets a unique identifier for the current managed thread.

P Thread.IsBackground

public bool IsBackground { get; set; }

Gets or sets a value indicating whether a thread is a background thread. If this property is true, the thread is a background thread, and it will not prevent the application from exiting when all threads are either stopped or are background threads. If this property is false, the thread is a foreground thread, and the application will not exit until the thread has terminated.

M Thread.Start()

public void Start()

Updates the status of the thread to Running, unless the thread has already been started.

M Thread.Sleep(int millisecondsTimeout)

public static void Sleep(int millisecondsTimeout)

Suspends the current thread for the specified number of milliseconds.

M CancellationTokenSource.Cancel()

public void Cancel()

Communicates a notification that this CancellationTokenSource has requested cancellation.

P CancellationTokenSource.IsCancellationRequested

public bool IsCancellationRequested { get; }

Gets whether cancellation has been requested for this CancellationTokenSource.

P CancellationTokenSource.Token

public CancellationToken Token { get; }

Gets the CancellationToken associated with this CancellationTokenSource.

M ManualResetEvent.Set()

public bool Set()

Sets the state of the event to signaled, allowing one or more waiting threads to proceed.

M ManualResetEvent.Reset()

public bool Reset()

Sets the state of the event to nonsignaled, causing threads to block.

M ManualResetEvent.WaitOne()

public bool WaitOne()

Blocks the current thread until the current instance receives a signal.

M SemaphoreSlim.WaitAsync()

public Task WaitAsync()

Asynchronously decrements the current SemaphoreSlim count, blocking the calling thread until it can do so.

M SemaphoreSlim.Release()

public int Release()

Releases the semaphore one count, thereby releasing one waiting thread or fulfilling one asynchronous wait.