C# Multithreading

C# Multithreading

C# Multithreading is a technique used to run a single set of program code, which can be used by several processors at different stages of execution of that program life cycle.

A thread is the execution track of the program in which it performs its complete or partial tasks. The thread handle the flow control of every process attached to it. For a complex application which is quite time consuming, it is the better way to properly manage different threads of execution of a program at the required time as well as the resources when they are demanded (not always).
A thread can more properly be term as a small process. The CPU of modern processors are implementing the usage and management of the execution of concurrent threads to fully utilize the CPU cycle and the proper execution of the applications.

In simple words, a thread is a running instance of a program. For example, a Microsoft Application is running in its thread or process of execution.
In order to make our application work with different smaller instances, we have to use a technique known as Multithreading.

The thread life cycle comprises of four events:

1.Unstarted State—

The instance of a thread is created but the thread hasn’t started its execution.

2.Ready State—

The thread has harnessed its buckles and ready ti execute. Waiting for CPU Cycle.

3.Not Runnable State—

A thread does not execute if:

The Sleep method instantiated
The Wait method has been instantiated
Some I/O operations are blocking the thread.

4.Dead State—

The thread has either completed its execution or it is being terminated abruptly

Simple Thread Creation—-

The given example shows how to create a simple thread in a C# program and display its name. It is quite easier:


//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Threading;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            
         //Main thread by the name mainThread
         Thread mainThread = Thread.CurrentThread;
         // assigning the name
         mainThread.Name = "MyThread";
         // Printing the name
         Console.WriteLine("The Thread is main and its name is: {0}", mainThread.Name);
         
        }
    }
}




Output—

The Thread is main and its name is: MyThread

Using Multithreading—-

Using the technique of multithreading in C# is quite simpler. Consider and implement the following program:


//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Threading;

namespace Rextester
{
    
    public class MultipleThreads {

        public static void Thread1() 
        {
        
            Console.WriteLine("This is Thread1");
            
        }

        public static void Thread2() {
                
            
            Console.WriteLine("This is Thread2");
            
            
        }
}
    
    
    
    
    public class Program
    {
        public static void Main(string[] args)
        {
            
          Console.WriteLine("Starting the Threads.....");
            
            Thread.Sleep(1000);

                Thread t1 = new Thread(new ThreadStart(MultipleThreads.Thread1 ) );
                Thread t2 = new Thread(new ThreadStart(MultipleThreads.Thread2 ) );

                t1.Start();
                t2.Start();
         
        }
    }
}




Output—

Starting the Threads…..
This is Thread1
This is Thread2