C# Background Thread

Summary: in this tutorial, you’ll learn about the C# background threads and the difference between the background and foreground threads.

Introduction to C# background threads

.NET has two types of threads:

  • Foreground
  • Background

When a C# program runs, .NET creates a thread which is known as the main thread.

A foreground thread is a kind of thread that executes with the same priority as the main thread. A foreground thread keeps the application running until it is completed or aborted. In other words, the application will not be terminated if there are foreground threads running.

By default, the threads created using the Thread class are foreground threads. Also, the main thread is a foreground thread.

The following example illustrates how a foreground thread works:

static void DoWork()
{
    for (int i = 0; i < 5; i++)
    {
        Thread.Sleep(100);
        Console.WriteLine("Foreground thread running...");
        
    }
}

var t = new Thread(DoWork);
t.Start();


// Do some work in the main thread
for (int i = 0; i < 2; i++)
{
    Thread.Sleep(100);
    Console.WriteLine("Main thread running...");
    
}Code language: C# (cs)

Output:

Main thread running...
Foreground thread running...
Main thread running...
Foreground thread running...
Foreground thread running...
Foreground thread running...
Foreground thread running...Code language: C# (cs)

How it works.

First, define the DoWork() method that displays the same message fives time in every 100 ms:

static void DoWork()
{
    for (int i = 0; i < 5; i++)
    {
        Console.WriteLine("Foreground thread running...");
        Thread.Sleep(100);
    }
}Code language: C# (cs)

Second, create a new thread that executes the DoWork() method and start it:

var t = new Thread(DoWork);
t.Start();Code language: C# (cs)

Third, display the same message twice in the main thread every 100 ms:

// Do some work in the main thread
for (int i = 0; i < 2; i++)
{
    Console.WriteLine("Main thread running...");
    Thread.Sleep(100);
}Code language: C# (cs)

The output shows that after the main thread ends, the foreground thread is still running. Therefore, you see five messages from the foreground thread.

Unlike a foreground thread, a background thread is a kind of thread that runs in the background and does not keep the application running after the main thread ends.

In other words, regardless of whether any background threads are running, the application will end if all foreground threads terminate.

To change a thread from foreground to background, you use the IsBackground property of the Thread object. For example:

static void DoWork()
{
    for (int i = 0; i < 5; i++)
    {
        Thread.Sleep(100);
        Console.WriteLine("Background thread running...");
        
    }
}

var t = new Thread(DoWork);
t.IsBackground = true;
t.Start();


// Do some work in the main thread
for (int i = 0; i < 2; i++)
{
    Thread.Sleep(100);
    Console.WriteLine("Main thread running...");
}Code language: C# (cs)

Output:

Main thread running...
Background thread running...
Main thread running...Code language: C# (cs)

In this example, we change the thread from the foreground to the background. When the main thread is terminated, the background thread is also ended. Therefore, you see only one message from the background thread.

C# background thread applications

Background threads have the following practical applications:

  • Enhancing responsiveness of the application: Background threads can be used to do time-consuming operations like file I/O, network operations, or database queries without obstructing the main UI thread.
  • Parallel processing: The performance of the application can be greatly improved by using background threads to carry out parallel processing of huge datasets or complicated calculations.
  • Background processing: Tasks that don’t need user input, including data synchronization, backup, or maintenance, can be carried out in the background using background threads.
  • Multithreaded network applications: Background threads can be used in multithreaded network applications to handle multiple requests simultaneously.

Summary

  • A foreground thread keeps the application running until it is completed or aborted.
  • A background thread is a kind of thread that doesn’t keep the application running after the main thread has terminated.
  • Use the IsBackground property to change a thread from foreground to background.
Was this tutorial helpful ?