[EN] Discovering System.Threading namespace, the Thread class

This post is a part of preparation for 70-483 exam. Information written down here refers to the part Manage program flow.

.NET framework provides several mechanisms to write concurrency, parallel and asynchronous code. They all are included in System.Threading namespace. This namespace contains types that allow creating multithreaded applications. Today’s post is about the Thread class.

This topic is not required for the exam. However, the exam preparation materials sometimes references to it and I decided to write it down.

The Thread class is located in the System.Threading namespace and it has been a part of the .NET framework since version 1.1. It gives an ability to create new treads, manage their priority, get their status, tell Windows that the thread is long running, or configure other advanced options. In general using the Thread class you have control over all configuration options.

Basic usage of the Thread class

The code below is an example of an application using Thread class.

The output shows that those two threads run concurrently. I passed a lambda expression to the Thread class constructor but thread can be initialized within ThreadStart delegate as well.

Calling Start() method on a thread object runs the thread. This method has an overloaded version which takes object parameter and passes it to the thread. It’s useful when you want to pass additional data to the thread at start time.

At the bottom of each example you can notice Join() method call. This method blocks the calling thread to wait till the thread finishes execution.

Stopping the thread

Thread class contains an Abort method which raises a ThreadAbortException on the target thread. It is not the best choice because it can potentially leave a corrupt state and make your application unusable. Using a shared variable that both your target and your calling thread can access is an alternative to stop a thread. The example below shows that concept.

ThreadStaticAttribute and ThreadLocal

Sometimes it’s reasonable to use static fields in the code. However, if the code is executed in multiple threads you can run into a problem where the field is influenced by each thread separately and it can lead you to unexpected troubles. Look at code above.

The correct result should be 6 for the additional thread and 13 for the main thread. Instead both results are the same. Using ThreadStaticAttribute fixes this issue. This annotation gives each thread its unique copy of the field.

Unfortunately, when you want to initialize static field locally in each thread then ThreadStaticAttribute is not enough. Code like this:

Will set sum field to 10 only for the current thread, the others will still have 0. ThreadLocal class deals with that problem. It takes a delegate as a parameter and initializes the value.

IsBackground property

One other thing worth to mention is that thread can be run either as a foreground or a background thread. There is only a slight difference, as MSDN portal says:

Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.

Thread object has IsBackground property which defines whether the thread is a background or a foreground thread. The example below shows the usage of this property.

The difference is that the program finishes when the main thread ends its execution and thread t1 is terminated immediately. If the flag would be set to false (default value) then the thread t1 would count to 10.


That’s all for today. I hope the topic is covered well enough. In further posts I will focus on Thread Pool class and Task Parallel Library. Stay tuned.

Of course all above examples are available at github.

3 comments on “[EN] Discovering System.Threading namespace, the Thread class
  1. Pingback: [EN] Exam 70-483 Programming in C# - Introduction - Tymoteusz Kęstowicz .NET developer blog

Say something

Your email address will not be published. Required fields are marked with a grey bar.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Current ye@r *