This post will contain my notes on the C# System.Threading namespace. For an extremely basic introduction to Threads, see my previous post [LINK]

Thread Class

The Thread Class is a found in the System.Threading namespace and can be used to create new Threads, get their status and manage their priorities.

The following is an example of how to use the Thread class.

This is probably not the most useful example of how to use a Thread, but it’s an example.

We create a Thread (called t), and give it a method to run (SomeThreadedMethod). Then we tell the t to start processing, then we tell Main() to wait until t has finished before moving on.

We use the Thread.Sleep method in SomeThreadedMethod to tell Windows that the thread that it is running in has finished. Without this, the thread would continue to run until the Windows scheduler has closed it’s time slot.

Thread Types

There are two types of Threads:

  • Foreground Thread
  • Background Thread

A foreground thread is used to keep an application going, its the thread that powers the GUI for an application. Google Chrome’s browser window is an example of a Foreground thread.

A background thread is meant for processing data without locking up the foreground thread.

Again, another gross over simplification coming

Imagine that you’re downloading a file with Google Chrome. There are (among other foreground and background threads) two Threads in use here:

  • 1 Foreground thread keeping Chrome’s browser window responsive (opening new tabs, scrolling up and down, etc.)
  • 1 Background thread performing the download action

The file download is performed on a background thread, because we don’t want Chrome to become unresponsive while downloading the file.

In .NET and C#, as soon as all Foreground threads are finished, the application that spawned them will close.

Technically is the CLR (Common Language Runtime) that closes the application

Passing Data

To pass data to a thread, we do the following:

This extremely simple example created a new thread using the ParameterizedThreadStart option which tells the Thread that we want to pass it some data when we start it. Then we do just that, we pass it a string to output to the Console when we start the thread.

A short note on the Console:

The console uses synchronisation to allow us to output to it from different threads.

If we wanted to output from the Main thread whilst t was running, then the Console would wait for t to finish writing to it before allowing Main to write to it.

Stopping Threads

The best way to stop a thread (without causing a ThreadAbortException) is to use a shared variable:

Threads have their own call stack, featuring the values of all local variables and any methods called during the processing of that Thread.

If you need to have multiple threads in an application then hand rolling them isn’t that great a solution. This is because you need to know, ahead of time, just how many threads you’ll need. This isn’t always possible, so to get around this, we use the ThreadPool class.

More on that next time.

Related Post

Jamie is a .NET developer specialising in ASP.NET MVC websites and services, with a background in WinForms and Games Development. When not programming using .NET, he is either learning about .NET Core (and usually building something cross platform with it), speaking Japanese to anyone who'll listen, learning about languages, writing for this blog, or writing for a blog about Retro Gaming (which he runs with his brother)