Multithreading in C#

A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.
Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.
So far we wrote the programs where a single thread runs as a single process which is the running instance of the application. However, this way the application can perform one job at a time. To make it execute more than one task at a time, it could be divided into smaller threads.

Thread Life Cycle:

The life cycle of a thread starts when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution.
Following are the various states in the life cycle of a thread: 

- The Unstarted State: It is the situation when the instance of the thread is created but the Start method is not called.
- The Ready State: It is the situation when the thread is ready to run and waiting CPU cycle.
- The Not Runnable State: A thread is not executable, when:
     - Sleep method has been called
     - Wait method has been called
     - Blocked by I/O operations
- The Dead State: It is the situation when the thread completes execution or is aborted.

The Main Thread:

In C#, the System.Threading.Thread class is used for working with threads. It allows creating and accessing individual threads in a multithreaded application. The first thread to be executed in a process is called the main thread.
When a C# program starts execution, the main thread is automatically created. The threads created using the Thread class are called the child threads of the main thread. You can access a thread using the CurrentThread property of the Thread class.

Example of Multithreaded application in C#:
Now let's create a Windows Forms application and add two buttons: Thread1Button, Thread2Button.
Then we add this directive:

using System.Threading;


And declare a random variable:

Random rd;


Now, in Form load event put this code:

private void Form1_Load(object sender, EventArgs e)
{
    rd = new Random();
}


Then in Thread1Button, Thread2Button add this code:

private void Thread1Button_Click(object sender, EventArgs e)
{
    Thread thread = new Thread(t =>
    { 
        for(int i=0; i<100;i++)
        {
            int width = rd.Next(0, this.Width);
            int height = rd.Next(50, this.Height);
            this.CreateGraphics().DrawEllipse(new Pen(Brushes.Red, 1), new Rectangle(width, height, 10, 10));
            Thread.Sleep(100);
        }
        MessageBox.Show("Thread 1 completed");

    }) { IsBackground = true };
    thread.Start();
}
private void Thread2Button_Click(object sender, EventArgs e)
{
    Thread thread = new Thread(t =>
    {
        for (int i = 0; i < 100; i++)
        {
            int width = rd.Next(0, this.Width);
            int height = rd.Next(50, this.Height);
            this.CreateGraphics().DrawEllipse(new Pen(Brushes.Blue, 1), new Rectangle(width, height, 10, 10));
            Thread.Sleep(100);
        }
        MessageBox.Show("Thread 2 completed");

    }) { IsBackground = true };
    thread.Start();
}

Now let's run the application click Thread1Button, then Thread2Button, we see red and blue circles appears simultaneously.





Reference:
C# multithreading 
Multithreading in C# Multithreading in C# Reviewed by Bloggeur DZ on 10:00 Rating: 5

Aucun commentaire:

Fourni par Blogger.