articles

Home / DeveloperSection / Articles / Multithreading in C#

Multithreading in C#

Anonymous User7093 26-Aug-2014

Previously, we learn about Thread Methods in Join Sleep and Interrupt methods in c# Threading  now we see Multithreading in c#

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 is defined in the System. Threading namespace. 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 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#

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

 

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#

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#

 

Suspend a Thread

Pauses the execution 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 and Set

Priority in C#


You can also read similer post

https://www.mindstick.com/Articles/144/multithreading-in-c-sharp



Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By