---
title: "The Threading in C# with Example"  
description: "The Threading in C# with ExampleC# supports parallel execution of job or program code through multithreading. Multithreading contains two or more pr"  
author: "Anonymous User"  
published: 2011-08-29  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/630/the-threading-in-c-sharp-with-example  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# The Threading in C# with Example

## 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:

![Threading in C# with Example](https://www.mindstick.com/mindstickarticle/13cd0056-e8ea-4637-b7c8-0146ea71fe4a/images/06da1c6a-6c6e-476b-b80d-3613ee5d274d.png)

#### 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](https://www.mindstick.com/articles/977/login-form-in-asp-dot-net-with-c-sharp) 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](https://www.mindstick.com/mindstickarticle/13cd0056-e8ea-4637-b7c8-0146ea71fe4a/images/62787b7d-2f19-411b-91a3-643577f341f6.png)

##### Example: Create thread with taking parameter or Parameterized Thread.

We can also create a parameterized thread for [creating parameterized thread](https://www.mindstick.com/articles/334/how-to-use-track-bar-control-in-c-sharp) 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](https://www.mindstick.com/mindstickarticle/13cd0056-e8ea-4637-b7c8-0146ea71fe4a/images/b58e572b-d1c0-4e67-83be-ef03a13c8392.png)

#### 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:

![Threading in C# with Example](https://www.mindstick.com/mindstickarticle/13cd0056-e8ea-4637-b7c8-0146ea71fe4a/images/a0205f0f-c0a0-4aa2-b1f0-52f1819b55fa.png)

\

---

Original Source: https://www.mindstick.com/articles/630/the-threading-in-c-sharp-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
