---
title: "Multithreading in C#"  
description: "In this article I’m explaining about multithreading in c#."  
author: "Anonymous User"  
published: 2014-08-26  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1488/multithreading-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Multithreading in C#

Previously, we learn about Thread Methods in Join Sleep and Interrupt [methods in c#](https://www.mindstick.com/forum/33925/difference-between-dispose-and-finalize-methods-in-c-sharp) [Threading](https://www.mindstick.com/blog/784/multithreading-with-c-sharp) now we see [Multithreading in c#](https://www.mindstick.com/forum/159901/how-can-i-work-with-threads-and-multithreading-in-c-sharp-please-provide-a-sample)

In this article I’m explaining about multithreading in c#.

Multithreading is used to perform multiple tasks at the same time. Tasks with the potential of holding up other tasks can execute on separate threads, a process known as multithreading. Or, it's basically trying to do more than one thing at a time within a process. In .NET, threading [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) is defined in the System. Threading [namespace](https://www.mindstick.com/articles/28/namespace-in-dot-net). So you have to define System. Threading namespace before using any thread classes.

A C# client program starts in a single thread created automatically by the CLR and [operating system](https://answers.mindstick.com/qa/114592/which-mobile-operating-system-is-better-for-privacy-ios-or-android) that is called the main thread, and is made multithreaded by creating additional threads. Here's a simple example and its output.

```
using System.Threading; namespace MultiThreading{    class Program    {        static void Main(string[] args)        {            Thread thread = new Thread(new ThreadStart(Display));            thread.Start();            for (int i = 0; i <= 10; i++)            {                Console.WriteLine("This is Main Thread");            }        }        public static void Display()        {            for (int i = 0; i <= 10; i++)            {                Console.WriteLine("This Thread is Created By Main Thread");            }        }    }}
```

##### Output

![Multithreading in C#](http://www.mindstick.com/MindStickArticle/7d61a34a-5c86-480c-957b-bce839afe79b/Images/image001.png)

In this example main threads creates a new thread on which runs a method that repeatedly print the “This Thread is Created by Main Thread” simultaneously the main thread repeatedly print string “This is main thread”

Abort a Thread

The Thread class's Abort method is called to abort a thread. Make sure you check the IsAlive [property](https://www.mindstick.com/articles/198559/an-ownership-of-property-in-hua-hin) before Abort.

```
static void Main(string[] args)        {            Thread thread = new Thread(new ThreadStart(Display));            if (thread.IsAlive)            {                thread.Start();                thread.Abort();            }            for (int i = 0; i <= 10; i++)            {                Console.WriteLine("This is Main Thread");            }        }        public static void Display()        {            for (int i = 0; i <= 10; i++)            {                Console.WriteLine("This Thread is Created By Main Thread");            }        }
```

##### Output

![Multithreading in C#](http://www.mindstick.com/MindStickArticle/7d61a34a-5c86-480c-957b-bce839afe79b/Images/image002.png)

##### Pausing a Thread

\

You can use Thread. Sleep () for pausing a thread.

```
using System.Threading; namespace MultiThreading{    class Program    {        static void Main(string[] args)        {            Thread thread = new Thread(new ThreadStart(Display));            thread.Start();            for (int i = 0; i <= 10; i++)            {                Console.WriteLine("This is Main Thread");                Thread.Sleep(2000);            }        }        public static void Display()        {            for (int i = 0; i <= 10; i++)            {                Console.WriteLine("This Thread is Created By Main Thread");                Thread.Sleep(4000);            }        }    }}
```

##### Output

![Multithreading in C#](http://www.mindstick.com/MindStickArticle/7d61a34a-5c86-480c-957b-bce839afe79b/Images/image003.png)

In this example main thread creates a new thread which is paused by the Thread. Sleep () 4second main thread is also paused by Thread. Sleep () for 2seconds.

Thread Highest Priority

Thread class ThreadPriority property is used to set the threads priority threads priority are below.

1. Normal

2. AboveNormal

3. BelowNormal

4. Highest

5. Lowest

```
using System.Threading; namespace MultiThreading{    class Program    {        static void Main(string[] args)        {            Thread thread = new Thread(new ThreadStart(Display));            thread.Priority = ThreadPriority.Lowest;            thread.Start();            for (int i = 0; i <= 10; i++)            {                Console.WriteLine("This is Main Thread");            }        }        public static void Display()        {            for (int i = 0; i <= 10; i++)            {                Console.WriteLine("This Thread is Created By Main Thread");            }        }    }}
```

##### Output

![Multithreading in C#](http://www.mindstick.com/MindStickArticle/7d61a34a-5c86-480c-957b-bce839afe79b/Images/image004.png)

##### Suspend a Thread

Pauses the [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) of the thread temporarily. The Suspend method of the Thread class suspends a thread.The thread is suspended until the Resume method is called.

```
if (thread.ThreadState == ThreadState.Running) {       thread.Suspend();} 
```

##### Resume a suspended Thread

Resume the execution of the thread temporarily The Resume method is called to resume a suspended thread. If the thread is not suspended, the Resume method will have no effect.

```
if (thread.ThreadState == ThreadState.Suspended) {       thread.Resume();} 
```

\

You may also want to read: Multithreading in C#

\

In my next post, we are going to learn about : Thread [Synchronization](https://www.mindstick.com/articles/11983/synchronization-in-c-sharp) and Set

Priority in C#

## *\*

You can also read similer post

[https://www.mindstick.com/Articles/144/multithreading-in-c-sharp](https://www.mindstick.com/Articles/144/multithreading-in-c-sharp)\

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