In C#, the System.Threading.Thread class is utilized for working with thread. It permits making and getting to individual thread in a multithreaded 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 button(image given below).
Step 2:-Write code on button click and Form1_Load like given example below
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace ThreadApplication { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnTh1_Click(object sender, EventArgs e) { Thread th = 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, 100, 100)); Thread.Sleep(100); } }) { IsBackground = true }; th.Start(); } Random rd; private void Form1_Load(object sender, EventArgs e) { rd = new Random(); } private void btnTh2_Click(object sender, EventArgs e) { Thread th = 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, 100, 100)); Thread.Sleep(100); } }) { IsBackground = true }; th.Start(); } private void btnTh3_Click(object sender, EventArgs e) { Thread th = 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.Green, 1), new Rectangle(Width, Height, 100, 100)); Thread.Sleep(100); } }) { IsBackground = true }; th.Start(); } } }
Step 3:- Run application see output like this.
Description :- In this example i've created 3 button to create and manage new thread with different color structure,example to show eclipse graph inside form on individual thread start.
You have just read an article that categorized by title C#
by title How to work on multi-threading in C# ?. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/06/part-21how-to-work-on-multi-threading.html. Thank You!
Author:
Bikesh Srivastava - Wednesday, June 22, 2016
There are currently no comments for "How to work on multi-threading in C# ?"
Post a Comment