How To Use Multi-Threading in C#

In C#, the System.Threading.Thread class is utilized for working with the thread. It permits making and getting to the individual thread in a multi-threaded application. The principal thread to be executed in a procedure is known as the principle thread. At the point when a C# program begins execution, the fundamental thread is consequently made.

Example with image shown below, 
Step 1: Create a Window form with three buttons (image given below).
 
 

Step 2: Write the code on the button click and Form1_Load like the example, given below:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Threading;  
  10.   
  11. namespace ThreadApplication  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         private void btnTh1_Click(object sender, EventArgs e)  
  21.         {  
  22.              
  23.               
  24.             Thread th = new Thread(t =>  
  25.             {  
  26.   
  27.                 for (int i = 0; i <= 100; i++)  
  28.                 {  
  29.                     int Width = rd.Next(0, this.Width);  
  30.                     int Height = rd.Next(50, this.Height);  
  31.                     this.CreateGraphics().DrawEllipse(new Pen(Brushes.Red, 1), new Rectangle(Width, Height, 100, 100));  
  32.                     Thread.Sleep(100);  
  33.                 }  
  34.   
  35.   
  36.             }) { IsBackground = true };  
  37.             th.Start();  
  38.         }  
  39.         Random rd;  
  40.         private void Form1_Load(object sender, EventArgs e)  
  41.         {  
  42.             rd = new Random();  
  43.   
  44.         }  
  45.   
  46.         private void btnTh2_Click(object sender, EventArgs e)  
  47.         {  
  48.   
  49.             Thread th = new Thread(t =>  
  50.             {  
  51.   
  52.                 for (int i = 0; i <= 100; i++)  
  53.                 {  
  54.                     int Width = rd.Next(0, this.Width);  
  55.                     int Height = rd.Next(50, this.Height);  
  56.                     this.CreateGraphics().DrawEllipse(new Pen(Brushes.Blue, 1), new Rectangle(Width, Height, 100, 100));  
  57.                     Thread.Sleep(100);  
  58.                 }  
  59.   
  60.   
  61.             }) { IsBackground = true };  
  62.             th.Start();  
  63.         }  
  64.   
  65.         private void btnTh3_Click(object sender, EventArgs e)  
  66.         {  
  67.               
  68.             Thread th = new Thread(t =>  
  69.             {  
  70.   
  71.                 for (int i = 0; i <= 100; i++)  
  72.                 {  
  73.                     int Width = rd.Next(0, this.Width);  
  74.                     int Height = rd.Next(50, this.Height);  
  75.                     this.CreateGraphics().DrawEllipse(new Pen(Brushes.Green, 1), new Rectangle(Width, Height, 100, 100));  
  76.                     Thread.Sleep(100);  
  77.                 }  
  78.   
  79.   
  80.             }) { IsBackground = true };  
  81.             th.Start();  
  82.         }  
  83.     }  
  84.   
  85. }  
Step 3:- Run the Application and see the output, given below:



Description

In this example, I created three buttons to create and manage new threads with the different color structure,
 with an example to show an eclipse graph inside the form on the individual thread start. 
Ebook Download
View all
Learn
View all