6 min read

In this article by Eugene Agafonov, author of the book Multithreading with C# Cookbook – Second Edition, we will cover the basic tasks to work with threads in C#. You will learn the following recipes:

  • Creating a thread in C#
  • Pausing a thread
  • Making a thread wait

(For more resources related to this topic, see here.)

Creating a thread in C#

Throughout the following recipes, we will use Visual Studio 2015 as the main tool to write multithreaded programs in C#. This recipe will show you how to create a new C# program and use threads in it.

There is a free Visual Studio Community 2015 IDE, which can be downloaded from the Microsoft website and used to run the code samples.

Getting ready

To work through this recipe, you will need Visual Studio 2015. There are no other prerequisites.

How to do it…

To understand how to create a new C# program and use threads in it, perform the following steps:

  1. Start Visual Studio 2015. Create a new C# console application project.
  2. Make sure that the project uses .NET Framework 4.6 or higher; however, the code in this article will work with previous versions.
  3. In the Program.cs file, add the following using directives:
    using System;
    using System.Threading;
    using static System.Console;
  4. Add the following code snippet below the Main method:
    static void PrintNumbers()
    {
      WriteLine("Starting...");
      for (int i = 1; i < 10; i++)
      {
        WriteLine(i);
      }
    }
  5. Add the following code snippet inside the Main method:
    Thread t = new Thread(PrintNumbers);
    t.Start();
    PrintNumbers();
  6. Run the program. The output will be something like the following screenshot:

How it works…

In steps 1 and 2, we created a simple console application in C# using .Net Framework version 4.0. Then, in step 3, we included the System.Threading namespace, which contains all the types needed for the program. Then, we used the using static feature from C# 6.0, which allows us to use the System.Console type’s static methods without specifying the type name.

An instance of a program that is being executed can be referred to as a process. A process consists of one or more threads. This means that when we run a program, we always have one main thread that executes the program code.

In step 4, we defined the PrintNumbers method, which will be used in both the main and newly created threads. Then, in step 5, we created a thread that runs PrintNumbers. When we construct a thread, an instance of the ThreadStart or ParameterizedThreadStart delegate is passed to the constructor. The C# compiler creates this object behind the scenes when we just type the name of the method we want to run in a different thread. Then, we start a thread and run PrintNumbers in the usual manner on the main thread.

As a result, there will be two ranges of numbers from 1 to 10 randomly crossing each other. This illustrates that the PrintNumbers method runs simultaneously on the main thread and on the other thread.

Pausing a thread

This recipe will show you how to make a thread wait for some time without wasting operating system resources.

Getting ready

To work through this recipe, you will need Visual Studio 2015. There are no other prerequisites.

How to do it…

To understand how to make a thread wait without wasting operating system resources, perform the following steps:

  1. Start Visual Studio 2015. Create a new C# console application project.
  2. In the Program.cs file, add the following using directives:
    using System;
    using System.Threading;
    using static System.Console;
    using static System.Threading.Thread;
  3. Add the following code snippet below the Main method:
    static void PrintNumbers()
    {
      WriteLine("Starting...");
      for (int i = 1; i < 10; i++)
      {
        WriteLine(i);
      }
    }
    static void PrintNumbersWithDelay()
    {
      WriteLine("Starting...");
      for (int i = 1; i < 10; i++)
      {
        Sleep(TimeSpan.FromSeconds(2));
        WriteLine(i);
      }
    }
  4. Add the following code snippet inside the Main method:
    Thread t = new Thread(PrintNumbersWithDelay);
    t.Start();
    PrintNumbers();
  5. Run the program.

How it works…

When the program is run, it creates a thread that will execute a code in the PrintNumbersWithDelay method. Immediately after that, it runs the PrintNumbers method. The key feature here is adding the Thread.Sleep method call to a PrintNumbersWithDelay method. It causes the thread executing this code to wait a specified amount of time (2 seconds in our case) before printing each number. While a thread sleeps, it uses as little CPU time as possible. As a result, we will see that the code in the PrintNumbers method, which usually runs later, will be executed before the code in the PrintNumbersWithDelay method in a separate thread.

Making a thread wait

This recipe will show you how a program can wait for some computation in another thread to complete to use its result later in the code. It is not enough to use Thread.Sleep method because we don’t know the exact time the computation will take.

Getting ready

To work through this recipe, you will need Visual Studio 2015. There are no other prerequisites.

How to do it…

To understand how a program waits for some computation in another thread to complete in order to use its result later, perform the following steps:

  1. Start Visual Studio 2015. Create a new C# console application project.
  2. In the Program.cs file, add the following using directives:
    using System;
    using System.Threading;
    using static System.Console;
    using static System.Threading.Thread;
  3. Add the following code snippet below the Main method:
    static void PrintNumbersWithDelay()
    {
      WriteLine("Starting...");
      for (int i = 1; i < 10; i++)
      {
        Sleep(TimeSpan.FromSeconds(2));
        WriteLine(i);
      }
    }
  4. Add the following code snippet inside the Main method:
    WriteLine("Starting...");
    Thread t = new Thread(PrintNumbersWithDelay);
    t.Start();
    t.Join();
    WriteLine("Thread completed");
  1. Run the program.

How it works…

When the program is run, it runs a long-running thread that prints out numbers and waits two seconds before printing each number. But in the main program, we called the t.Join method, which allows us to wait for thread t to complete. When it is complete, the main program continues to run. With the help of this technique, it is possible to synchronize execution steps between two threads. The first one waits until another one is complete and then continues to work. While the first thread waits, it is in a blocked state (as it is in the previous recipe when you call Thread.Sleep).

Summary

In this article, we focused on performing some very basic operations with threads in the C# language. We covered a thread’s life cycle, which includes creating a thread, suspending a thread, and making a thread wait.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here