C# supports parallel execution of job or program code through multithreading. Multithreading contains two or more program codes which run concurrently without affecting to each other and each program codes is called thread. Use System.Threading namespce to implement multithreading in your program. Let’s see a simple example of creating multithreading in c sharp.

There are two types to create thread in C#, first one is through the Thread Class and second one is through the ThreadStart Delegate.

Example: Creating thread with help of Thread class

Here I am creating two threads which are executes concurrently without effecting to each other, first thread is main thread which prints ‘Thread: A ‘message and second one is ThreadB which prints ‘Thread: B’ message.

using System.Threading;
 
namespace ThreadTest
{
    class Program
    {
        static voidMain(string[] args)
        {
            //Creating thread object to strat it
            Thread th= newThread(ThreadB);
            Console.WriteLine("Threads started :");
            // Start thread B
            th.Start();
            //Thread A executes 10 times
            for (inti=0; i<10; i++)
            {
                Console.WriteLine("Thread : A");
            }
           
            Console.WriteLine("Threads completed");
            Console.ReadKey();
        }
        public staticvoidThreadB()
        {
           //Executes thread B 10 times
          for(inti=0;i<10;i++)
          {
             Console.WriteLine("Thread : B");
          }
        }
    }
}
Output:

Threading in C# with Example

Example: Creating thread with help ThreadStart Delegate.

When a managed thread is created, then method that executes within thread is represented by the ThreadStart delegate and passed into Thread constructor.  That is ThreadStart delegate represent a method which used in thread execution.

Let’s see the brief description of creating thread with help of ThreadStart delegate.
Example:
using System.Threading;
 
namespace ParameterizedThread
{
    class Program
    {
        static voidMain(string[] args)
        {
            // Create Thread class object and paas object of ThreadStart delegate with specified method its constructor
            Thread th=newThread(newThreadStart(MyMethod));
            th.Name="Child thread";
            // Start child thread
            th.Start();
            for (inti=0; i<20; i++)
            {
                Console.WriteLine("Main Thread");
            }
            Console.WriteLine("End Thread...");
            Console.ReadKey();
        }
        // Method used in thread execution
        public staticvoidMyMethod()
        {
           
            for (inti=0; i<10; i++)
            {
                Console.WriteLine("Child Thread");
            }
 
        }
    }
}

 

Output:

Threading in C# with Example

Example: Create thread with taking parameter or Parameterized Thread.

We can also create parameterized thread for creating parameterized thread writes the following code.

using System.Threading;
 
namespace ParameterizedThread
{
    class Program
    {
        static voidMain(string[] args)
        {
            // Create object of ParameterizedThreadStart to create parameterized thread 
            ParameterizedThreadStart paraThread= new ParameterizedThreadStart(MyMethod);
            // Paas the object ParameterizedThreadStart in Thread constructor 
            Thread th= newThread(paraThread);
            // Assign name of thread
            th.Name="Child Thread";
            Console.WriteLine("Start thread...");
            //Pass the parameter into child thread
            th.Start(th.Name);
            // Execute the main thread
            for (inti=0; i<10; i++)
            {
                Console.WriteLine("Main Thread");
            }
            Console.WriteLine("End Thread...");
            Console.ReadKey();
        }
        public staticvoidMyMethod(objectobj)
        {
            string name=Convert.ToString(obj);
            for (inti=0; i<10; i++)
            {
                Console.WriteLine(name);
            }
 
        }
    }
}
Output:

Threading in C# with Example

Thread Priority:

Thread priority specifies the scheduling priority of thread, and the executions of threads perform on its priority basis. When we create any thread and not assign its priority then by default normal priority will be assign for it. The thread priority does not affect the state of thread.

Example: Let’s see a brief example of thread priority.
using System.Threading;
 
namespace ForegroundThread
{
    class Program
    {
        static voidMain(string[] args)
        {
            // Create ThreadA object
            Thread threadA= newThread(newThreadStart(ChildThreadA));
            // Create ThreadB object
            Thread threadB= newThread(newThreadStart(ChildThreadB));
 
            threadA.Name= "Thread A";
            threadB.Name= "Thread B";
            // set highest priority of threadB
            threadB.Priority= ThreadPriority.Highest;
            threadA.Start();
            threadB.Start();
            Thread.CurrentThread.Name= "Main";
            for (inti=0; i<20; i++)
            {
                Console.WriteLine(Thread.CurrentThread.Name);
            }
            Console.ReadKey();
       }
        public staticvoidChildThreadA()
        {
            for (inti=0; i<10; i++)
            {
                Console.WriteLine("Child thread A:");
            }
        }
        public staticvoidChildThreadB()
        {
            for (inti=0; i<10; i++)
            {
                Console.WriteLine("Child thread B:");
            }
        }
    }
}
Output:

Threading in C# with Example


  Modified On Nov-23-2017 01:15:00 PM

Leave Comment