---
title: "Multithreading in csharp"  
description: "It is the path of Execution. Each thread defines the unique flow of control .Threads are lightweight process. Use of threads saves wastage of CPU cycl"  
author: "Manish Kumar"  
published: 2017-02-26  
updated: 2018-03-17  
canonical: https://www.mindstick.com/blog/11308/multithreading-in-csharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Multithreading in csharp

It is the path of [Execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net). Each thread defines the unique flow of control .Threads are lightweight process. Use of threads saves wastage of CPU cycle and [increase](https://www.mindstick.com/articles/12959/are-you-struggling-to-increase-the-profit-of-your-liquor-store) [efficiency](https://www.mindstick.com/articles/13091/12-tips-to-boost-your-studying-efficiency) of an [application](https://www.mindstick.com/articles/12824/calculator-application-in-android)\

It means it perform one task at a time

##### Multithreading

When we perform multiple task at the same time this is knowns as multithreading.\

##### Advantages-

Save time

[Optimize](https://www.mindstick.com/articles/43978/3-tips-to-optimize-any-website-and-get-to-the-top-of-google) the use of computer [resources](https://yourviews.mindstick.com/view/293/renovation-causes-wastage-of-resources) such as memory

Foreground thread- Foreground threads are those thread in which main application is exited but it continue running whenever foreground thread is not stopped.

[Background thread](https://www.mindstick.com/articles/620/foreground-and-background-thread-in-c-sharp-threading)-Background thread is die off if the application is finished

##### Sequential execution of program example

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading; namespace ConsoleApplication9{    class Program    {        static void Main(string[] args)        {            Funtion1();            Function2();                                }        public static void Funtion1()        {                                   Console.WriteLine("function 1 start");            for (int i = 1; i < 10; i++)            {                               Console.WriteLine("we are in function1");                Thread.Sleep(4000);                          }            Console.WriteLine("function 1 end");        }        public static void Function2()        {            Console.WriteLine("function 2 start");            for (int i = 1; i < 10; i++)            {                Console.WriteLine("we are in function2");                Thread.Sleep(4000);             }            Console.WriteLine("function 2 end");        }    }} 
```

##### When we run the application it gives the output as below

![Multithreading in csharp](https://www.mindstick.com/blogs/e510bbdc-0d3d-45f0-a25d-5ccf18cfdedb/images/df3f019b-d6cd-4737-bffc-b423d8cabeb2.png)\

In the above example we have saw sequential execution of program mean it run sequentially at first function 1 run after completing then it start function 2

##### Parallel execution of program

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;

namespace ConsoleApplication9{
    class Program    {
        static void Main(string[] args)        {
                        Thread fun1 = new Thread(Funtion1);            fun1.Start();
            Thread fun2 = new Thread(Function2);            fun2.Start();
        }        public static void Funtion1()        {
                   Console.WriteLine("function 1 start");            for (int i = 1; i < 10; i++)            {
                Console.WriteLine("we are in function1");                Thread.Sleep(4000);
            }            Console.WriteLine("function 1 end");        }        public static void Function2()        {            Console.WriteLine("function 2 start");            for (int i = 1; i < 10; i++)            {                Console.WriteLine("we are in function2");                Thread.Sleep(4000);
            }            Console.WriteLine("function 2 end");
        }
    }
}
```

##### \

##### You can also visit these related useful blogs

[Threading in C# with Example](https://www.mindstick.com/Articles/630/threading-in-c-sharp-with-example)[Multithreading in C#](https://www.mindstick.com/blog/509/multithreading-in-c-sharp)[Multithreading with C#](https://www.mindstick.com/blog/784/multithreading-with-c-sharp)

---

Original Source: https://www.mindstick.com/blog/11308/multithreading-in-csharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
