Multithreading – Introduction

By | 08/09/2021

In this post, we will see how to use multithreading in C# in order to have best performances in our applications.
Multithread is a very big argument and in this first post, we will just see how to start working with it.

But first of all, what does Multithreading mean?
From Microsoft web site:
A process is an executing program and operating system uses processes to separate the applications that are being executed. We can say that a Thread is a unit of a process that is responsible for executing the application code.
By default, a .NET program is started with a single thread, often called the primary thread. However, it can create additional threads to execute code in parallel or concurrently with the primary thread. These threads are often called worker threads.
We can use multiple threads to increase the responsiveness of our application and to take advantage of a multiprocessor or multi-core system to increase the application’s throughput.

In a nutshell a thread is an execution path of a program and we can use many threads (Multithreading) in order to run different tasks of a program at the same time.

We start creating a Console application where we will see how to check the name of Main thread:

using System;
using System.Threading;

namespace MultiThreading
{
    class Program
    {
        static void Main(string[] args)
        {
            // here we get the current Thread
            Thread mainThread = Thread.CurrentThread;
            // we define the Thread's name
            mainThread.Name = "Principal Thread";

            Console.WriteLine(mainThread.Name);
        }
    }
}



If we run the application, this will be the result:

Now, we will create two similar methods and we will run both, using the MainThread:

using System;
using System.Threading;

namespace MultiThreading
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread mainThread = Thread.CurrentThread;
            mainThread.Name = "Principal Thread";

            Count1();
            Count2();

            Console.WriteLine(mainThread.Name);
        }

        private static void Count1()
        {
            for(int i=1; i<11; i++)
            {
                Console.WriteLine($"Count1 {i}");
                Thread.Sleep(1000);
            }
        }

        private static void Count2()
        {
            for (int i = 11; i < 21; i++)
            {
                Console.WriteLine($"Count2 {i}");
                Thread.Sleep(1000);
            }
        }
    }
}



If we run the application, this will be the result:

We can see that, using a single thread, the system first run the Count1 method and then, when it is finished, it will run the method Count2.

Now, we will create two Threads in order to call the two methods in the same time:

using System;
using System.Threading;

namespace MultiThreading
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread mainThread = Thread.CurrentThread;
            mainThread.Name = "Principal Thread";

            // We define two new threads and we pass the method that we want 
            // to run for each thread
            Thread firstThread = new Thread(Count1);
            Thread secondThread = new Thread(Count2);

            // we run the new threads
            firstThread.Start();
            secondThread.Start();

            Console.WriteLine(mainThread.Name);
        }
        ...
        ...
        ...
    }
}



Now, if we run the application this will be the result:

Perfect! We can see that system runs in the same time the two Thread!

The last thing I want to show in this first post, is how to call a method in a Thread with a parameter in input:

using System;
using System.Threading;

namespace MultiThreading
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread mainThread = Thread.CurrentThread;
            mainThread.Name = "Principal Thread";

            // We define two new thread and we pass the method that we want 
            // to run for each thread
            Thread firstThread = new Thread(Count1);
            Thread secondThread = new Thread(Count2);
            // when the method has parameters in input, we have to create
            // the Thread using Lambda expression
            Thread thirdThread = new Thread(() => Count3(31));

            // we run the new threads
            firstThread.Start();
            secondThread.Start();
            thirdThread.Start();

            Console.WriteLine(mainThread.Name);
        }
        ...
        ...
        ...
        
        // Definition of a new method called Count3, with a parameter in input 
        private static void Count3(int value)
        {
            for (int i = 21; i < value; i++)
            {
                Console.WriteLine($"Count3 {i}");
                Thread.Sleep(1000);
            }
        }
    }
}



If we run the application, this will be the result:



Leave a Reply

Your email address will not be published. Required fields are marked *