The Threading in C# with Example
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 code is called thread. Use System.Threading namespace to implement multithreading in your program. Let’s see a simple example of creating multithreading in c sharp.
There are two types to create a thread in C#, the first one is through the Thread Class and the second one is through the ThreadStart Delegate.
Example: Creating a thread with help of Thread class
Here I am creating two threads that are executed concurrently without effecting to each other, a first thread is the main thread which prints ‘Thread: A ‘message and the 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:
Example: Creating thread with help ThreadStart Delegate.
When a managed thread is created, then a method that executes within a 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:
Example: Create thread with taking parameter or Parameterized Thread.
We can also create a 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:
Thread Priority:
Thread priority specifies the scheduling priority of threads, 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 assigned for it. The thread priority does not affect the state of a 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:
kelly ng
01-Jun-2015Anonymous User
29-Sep-2011Amit Singh
28-Sep-2011