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. namespace ThreadApplication
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void btnTh1_Click(object sender, EventArgs e)
  19. {
  20. Thread th = new Thread(t =>
  21. {
  22. for (int i = 0; i <= 100; i++)
  23. {
  24. int Width = rd.Next(0, this.Width);
  25. int Height = rd.Next(50, this.Height);
  26. this.CreateGraphics().DrawEllipse(new Pen(Brushes.Red, 1), new Rectangle(Width, Height, 100, 100));
  27. Thread.Sleep(100);
  28. }
  29. }) { IsBackground = true };
  30. th.Start();
  31. }
  32. Random rd;
  33. private void Form1_Load(object sender, EventArgs e)
  34. {
  35. rd = new Random();
  36. }
  37. private void btnTh2_Click(object sender, EventArgs e)
  38. {
  39. Thread th = new Thread(t =>
  40. {
  41. for (int i = 0; i <= 100; i++)
  42. {
  43. int Width = rd.Next(0, this.Width);
  44. int Height = rd.Next(50, this.Height);
  45. this.CreateGraphics().DrawEllipse(new Pen(Brushes.Blue, 1), new Rectangle(Width, Height, 100, 100));
  46. Thread.Sleep(100);
  47. }
  48. }) { IsBackground = true };
  49. th.Start();
  50. }
  51. private void btnTh3_Click(object sender, EventArgs e)
  52. {
  53. Thread th = new Thread(t =>
  54. {
  55. for (int i = 0; i <= 100; i++)
  56. {
  57. int Width = rd.Next(0, this.Width);
  58. int Height = rd.Next(50, this.Height);
  59. this.CreateGraphics().DrawEllipse(new Pen(Brushes.Green, 1), new Rectangle(Width, Height, 100, 100));
  60. Thread.Sleep(100);
  61. }
  62. }) { IsBackground = true };
  63. th.Start();
  64. }
  65. }
  66. }
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.