C# language: Concurrency(multithreading)

Typically, applications or programs on a computer or phone perform more than one operation, this is called concurrency, we will first discuss the basics of concurrency, ie threads and tasks, and in the next lesson we will go to asynchrony.

The most common examples of concurrency:

  1. In mobile applications, WPF, websites, tasks must be run concurrently with the code responsible for the user interface to maintain responsive.
  2. The operating system performs some tasks concurrently, eg if you have a computer turned on you usually have more than one program enabled, if the operating system didn’t perform these tasks concurrently, then we could have only one program switched on at a time.
  3. In portals or online stores, when more than one user uses them in an instant, concurrency allows processing more than one client’s request at once.



A thread is an execution path that can be executed independently of other paths eg the main thread in some program is eg calculation of an expression so it is single-threaded, and if it were multithreaded, many threads could be running in one moment eg would calculate a value in the same for a moment it could do something else in the background, some a method whose result would display eg after calculating the value.

In the main thread in the “Main” method we create three other threads, methods that display characters in the console and you can see that at the same time characters from the “Main” method are displaying, i.e. our program is multithreaded.

You can also wait for the thread to finish working, then you need to add the Join() method

You can see the differences that the threads are executing now in turn.

We can also put the thread to sleep for a while, let’s put the thread y to sleep:

In this example, the WriteY() method will be executed immediately, then we have to wait 3 seconds to execute the next threads, but the time must be given in milliseconds.

While there is a problem with threads, look at the example below:

At the same time, two threads are executed that start the same method, this will display the word “Done” twice in the console.

How can you check the security of threads? The keyword “lock” is used for this. So we will secure our threads in the example below.

The solution used in the above example guarantees that one block can be put on one thread at a time, which means that the word “Done” will be displayed once.

Sometimes there is a need to pass data to the thread. In this case, the lambda expression is the easiest to use:



The thread is a low-level tool designed to provide concurrency and therefore has some limitations.

  1. It is easy to pass data to it, but it is not easy to get a return value from it.
  2. You can not tell a thread to execute another thread when it ends. Instead, it is necessary to use the Join() method, which means blocking the thread at that time.

Direct use of threads also worsens performance.

The Task class is the solution to all these problems.


Starting the task

Tasks are created in the following way:

This record is the equivalent of threads in this form:



call Wait() method is the equivalent of the join() method in threads:


Return value

In the threads is difficult to do, trivial in tasks:



The continuations could be translated into the sentence “after finishing doing something else”. There are two ways to define the continuation for the task, the first is important because it is used in asynchronous functions, what you will learn in the next lesson, below is an example:

The second way to add a continuation is to call the ContinueWith() method:



The Task.Delay() method is the asynchronous equivalent of Thread.Sleep():

This content also you can find on my steemit blog: https://steemit.com/programming/@slawas/c-language-concurrency-multithreading

And on medium: https://medium.com/@sawomirkowalski/c-language-concurrency-multithreading-40103c82284

We have reached the end, the knowledge in this lesson will be need in the next one, so understand the material in it well.

If that post was useful for you share it with your friends :)

Post a comment

Notify about