I have 4+ experience and working as software Engineer with Hytech professionals India PVT ltd and also working as Consultant,Trainer,Blogger and Author on Microsoft technologies,Also I have Trained more than 500+ IT professionals.I have knowledge about Asp.net,C#,SQL,Project management,replication,MVC,WebApi,SSRS,PostMan,TFS,SVN,Swagger,WMI,Agile,SignalR,Angularjs,IIS configuration.

Part 21::How to work on multi-threading in C# ?

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:-                                                                                                                      Thread Flow 
Step 1:-Create a window form with three button(image given below).

                        Design  window

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.

                        Output window


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 Part 21::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, 22 June 2016

2 comments to "Part 21::How to work on multi-threading in C# ?"

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. dot net training institute in velachery | top10 dot net training institutes in chennai

    ReplyDelete
  2. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training in chennai velachery | dot net training institute in velachery

    ReplyDelete

Life Is Complicated, But Now programmer Can Keep It Simple.