CodeBlanks Logo Icon CodeBlanks

The Thread class

C# course · Expert · Lesson 19/47

Learn to create threads with System.Threading.Thread.

The Thread class

The System.Threading.Thread class represents an operating-system level thread. Thread t = new Thread(Work); creates a new thread; t.Start(); starts it.

Each thread has its own stack and runs independently. Modern C# prefers Task/async in most cases, but Thread is still used for low-level control.

Creating a thread

The Thread constructor takes a ThreadStart or ParameterizedThreadStart delegate. new Thread(() => { /* work */ }) is the most common form.

Once started, a thread runs in parallel with the main thread; both advance in their own flow. When ordering matters, you need synchronization (lock, Interlocked, signals).

Exercise Start a thread and Join

Create a Thread t = new Thread(() => { });. Start it with t.Start();, wait with t.Join();. Finally print Console.WriteLine("Done");.

Show example solution
Thread t = new Thread(() => { }); t.Start(); t.Join(); Console.WriteLine("Done");
Example solution:
using System;
using System.Threading;

class Program
{
    static void Main()
    {
        Thread t = new Thread(() => { });
        t.Start();
        t.Join();
        Console.WriteLine("Done");
    }
}

Test yourself

  1. How is a thread started?

    • Start()
    • Run()
    • Begin()
    • Execute()
    Show answer

    Start() — Correct. The Start() method runs the thread.

  2. Create a thread

    Thread t = new ____(() => { Console.WriteLine("İş"); });
    Show answer
    Thread t = new Thread(() => { Console.WriteLine("İş"); });
  3. What does the Thread constructor take?

    • An int
    • A string
    • A delegate (ThreadStart)
    • An array
    Show answer

    A delegate (ThreadStart) — Correct. Thread takes a delegate (ThreadStart).

All lessons in Expert