Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Async and Concurrency

The Thread class

Reference for System.Threading.Thread in C#: create and start threads, pass data, join, background threads, names, priorities and exception behavior.

Thread represents one operating-system thread that you create and control yourself. You give it a delegate, call Start(), and the delegate runs on the new thread while the caller continues. Join() waits for it to finish.

In modern code you rarely need Thread directly: Task.Run, async/await and Parallel use the thread pool and are cheaper. Reach for Thread when you need a dedicated, long-lived thread, a custom stack size, a specific name, or foreground/background control.

C#
var t = new Thread(() => Console.WriteLine("hello from a thread"));
t.Start();
t.Join();

Try it

C# Start, pass data and join

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Background threads and exceptions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public sealed class Thread
{
    public Thread(ThreadStart start);
    public Thread(ThreadStart start, int maxStackSize);
    public Thread(ParameterizedThreadStart start);

    public string? Name { get; set; }
    public bool IsBackground { get; set; }
    public bool IsAlive { get; }
    public ThreadState ThreadState { get; }
    public ThreadPriority Priority { get; set; }
    public int ManagedThreadId { get; }
    public static Thread CurrentThread { get; }

    public void Start();
    public void Start(object? parameter);
    public void Join();
    public bool Join(int millisecondsTimeout);
    public static void Sleep(int millisecondsTimeout);
    public static bool Yield();
}
Name Description
start The delegate to run: ThreadStart takes no arguments, ParameterizedThreadStart takes one object?.
maxStackSize Stack size in bytes; 0 uses the default. Use it only for deep recursion.
Name A label shown in the debugger. It can be set once.
IsBackground Background threads do not keep the process alive.
Priority A hint to the scheduler; operating systems often ignore it.

Returns: Join(int) returns true when the thread ended within the timeout. Start() returns nothing.

Errors: Start() throws ThreadStateException if called twice. An unhandled exception inside a thread terminates the whole process.

Usage notes

  • A thread can be started only once. To run the work again, create a new Thread.
  • Unhandled exceptions crash the process. Catch inside the thread delegate, then report the failure through a shared variable, a Task, or a queue.
  • Foreground by default. A forgotten foreground thread keeps the process running after Main returns. Mark helper threads IsBackground = true.
  • Threads are expensive. Each one reserves stack memory (about 1 MB by default on 64-bit Linux and Windows) and costs scheduling time. Do not create one per request or per item.
  • Memory visibility. Start() and Join() are synchronization points: data written before Start() is visible to the new thread, and data written by the thread is visible after Join().
  • Thread.Sleep blocks the thread. In async code use await Task.Delay. Do not use Thread.Abort: it is not supported in modern .NET.
  • Prefer Task. A task gives you results, exception propagation and cancellation for free. See Task and Task.Run.
  • Culture and context. CurrentThread is per-thread state; use AsyncLocal<T> for state that must follow await (see ThreadLocal and AsyncLocal).