Multithreading in C#

In c#, multithreading means executing the multiple threads simultaneously to perform multiple tasks at a time.

 

The perfect example of multithreading is the operating system. For example, in windows operating system we can open multiple applications like excel, word, notepad, and browser at a time and perform multiple tasks simultaneously. In an operating system, each application process will be taken care by separate threads.

 

As we discussed in threading in c#, by default every program will carry one thread to execute the application logic and that thread is called a Main thread. So, we can say that every program or application is by default a single-threaded model.

 

When we start a program execution, the Main thread will create automatically and it is responsible for executing the programming logic in a synchronous way that means one after another. So, the second process has to wait until the first process completes its execution, and it’s a time taking process.

 

To overcome this problem, multithreading has been introduced to execute the multiple tasks simultaneously (asynchronous way) by creating multiple threads in our application. The threads which we will create in our application will become a child threads for the Main thread.

 

In c#, the child threads are responsible for executing the tasks in an asynchronous way that means executing the multiple tasks simultaneously at a time.

 

In c#, we can create or access the threads using Thread class for that we need to import System.Threading namespace in our program.

 

To learn more about threads in c#, check Threading in c#.

C# Single Threaded Model Example

Following is the example of creating the application without having any child threads in c#.

 

using System;

using System.Threading;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            PrintInfo1();

            PrintInfo2();

            Console.ReadLine();

        }

        static void PrintInfo1()

        {

            for (int i = 1; i <= 4; i++)

            {

                Console.WriteLine("i value: {0}", i);

                Thread.Sleep(1000);

            }

            Console.WriteLine("First method completed");

        }

        static void PrintInfo2()

        {

            for (int i = 1; i <= 4; i++)

            {

                Console.WriteLine("i value: {0}", i);

            }

            Console.WriteLine("Second method completed");

        }

    }

}

If you observe the above example, we created two methods (PrintInfo1, PrintInfo2). Here, in PrintInfo1 method, we are making the Main thread to sleep for 1 second for every iteration and after 1 second it will resume its process and continue next iteration. So, the second method PrintInfo2 has wait (4 seconds) until the first method PrintInfo1 completes its execution.

 

When we execute the above program, we will get the result like as shown below.

 

i value: 1

i value: 2

i value: 3

i value: 4

First method completed

i value: 1

i value: 2

i value: 3

i value: 4

Second method completed

If you observe the above result, the second method (PrintInfo2) has waited until the first method (PrintInfo1) completes its execution.

 

The second method (PrintInfo2) has waited around 4 seconds to start its execution. So, to overcome the drawback of single-threaded model, we need to implement multithreading approach to execute the tasks simultaneously.

C# Multithreading Example

Following is the example of creating multiple threads to execute the multiple tasks simultaneously in c#.

 

using System;

using System.Threading;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            // Create child threads

            Thread t1 = new Thread(new ThreadStart(PrintInfo1));

            Thread t2 = new Thread(new ThreadStart(PrintInfo2));

            t1.Start();

            t2.Start();           

            Console.ReadLine();

        }

        static void PrintInfo1()

        {

            for (int i = 1; i <= 4; i++)

            {

                Console.WriteLine("i value: {0}", i);

                Thread.Sleep(1000);

            }

            Console.WriteLine("First method completed");

        }

        static void PrintInfo2()

        {

            for (int i = 1; i <= 4; i++)

            {

                Console.WriteLine("i value: {0}", i);

            }

            Console.WriteLine("Second method completed");

        }

    }

}

If you observe the above example, we created two threads (t1, t2) using Thread class to execute PrintInfo1 & PrintInfo2 methods simultaneously. When we start the program execution, both threads (t1, t2) will start simultaneously to perform assigned tasks.

 

When we execute the above program, we will get the result like as shown below.

 

i value: 1

i value: 1

i value: 2

i value: 3

i value: 4

Second method completed

i value: 2

i value: 3

i value: 4

First method completed

If you observe the above result, while the first method (PrintInfo1) is waiting for next iteration, the second method (PrintInfo2) has completed its execution instead of waiting for first method (PrintInfo1) execution.

 

This is how we can implement the multithreading in c# to execute the multiple tasks simultaneously based on our requirements.

Topics Covered