A complete program using Threads. Can this program be shorter

T

Tony Johansson

Hi!

Here is a complete working program that is using BackgroundWorker(threads).
When button 1 is pushed red square is being drawn on the panel.
If you push again you stop drawing.
It's the same principle with all three buttons.

Button 1 is drawing red square
Button 2 is drawing green circles
Button 3 is drawing blue rectangle

I find my program to long. is it possible to make it shorter by using some
alternative or perhaps better solution ?
What bother me is that each button is toggles between start Thread
respectivily stop Thread.
Can this be done in another way ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyBackgroundThreadForm());
}
}

partial class MyBackgroundThreadForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.BtnThread1 = new System.Windows.Forms.Button();
this.BtnThread2 = new System.Windows.Forms.Button();
this.BtnThread3 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.panel = new System.Windows.Forms.Panel();
this.ThreadWorker1 = new System.ComponentModel.BackgroundWorker();
this.ThreadWorker2 = new System.ComponentModel.BackgroundWorker();
this.ThreadWorker3 = new System.ComponentModel.BackgroundWorker();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(310, 13);
this.label1.TabIndex = 0;
this.label1.Text = "This example starts 3 different threads that
sleep for 100ms each";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 22);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(365, 13);
this.label2.TabIndex = 1;
this.label2.Text = "and during each iteration they create output in
the area at a random location";
//
// BtnThread1
//
this.BtnThread1.Location = new System.Drawing.Point(12, 78);
this.BtnThread1.Name = "BtnThread1";
this.BtnThread1.Size = new System.Drawing.Size(87, 23);
this.BtnThread1.TabIndex = 2;
this.BtnThread1.Text = "Start Thread 1";
this.BtnThread1.UseVisualStyleBackColor = true;
this.BtnThread1.Click += new
System.EventHandler(this.BtnThread1_Click);
//
// BtnThread2
//
this.BtnThread2.Location = new System.Drawing.Point(127, 78);
this.BtnThread2.Name = "BtnThread2";
this.BtnThread2.Size = new System.Drawing.Size(87, 23);
this.BtnThread2.TabIndex = 3;
this.BtnThread2.Text = "Start Thread 2";
this.BtnThread2.UseVisualStyleBackColor = true;
this.BtnThread2.Click += new
System.EventHandler(this.BtnThread2_Click);
//
// BtnThread3
//
this.BtnThread3.Location = new System.Drawing.Point(239, 78);
this.BtnThread3.Name = "BtnThread3";
this.BtnThread3.Size = new System.Drawing.Size(83, 23);
this.BtnThread3.TabIndex = 4;
this.BtnThread3.Text = "Start Thread 3";
this.BtnThread3.UseVisualStyleBackColor = true;
this.BtnThread3.Click += new
System.EventHandler(this.BtnThread3_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(9, 115);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 13);
this.label3.TabIndex = 5;
this.label3.Text = "Output from Threads";
//
// panel
//
this.panel.BackColor = System.Drawing.Color.White;
this.panel.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.panel.Location = new System.Drawing.Point(12, 131);
this.panel.Name = "panel";
this.panel.Size = new System.Drawing.Size(559, 360);
this.panel.TabIndex = 6;
//
// ThreadWorker1
//
this.ThreadWorker1.WorkerSupportsCancellation = true;
this.ThreadWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.ThreadWorker1_DoWork);
//
// ThreadWorker2
//
this.ThreadWorker2.WorkerSupportsCancellation = true;
this.ThreadWorker2.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.ThreadWorker2_DoWork);
//
// ThreadWorker3
//
this.ThreadWorker3.WorkerSupportsCancellation = true;
this.ThreadWorker3.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.ThreadWorker3_DoWork);
//
// MyBackgroundThreadForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(596, 503);
this.Controls.Add(this.panel);
this.Controls.Add(this.label3);
this.Controls.Add(this.BtnThread3);
this.Controls.Add(this.BtnThread2);
this.Controls.Add(this.BtnThread1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "MyBackgroundThreadForm";
this.Text = "MyBackgroundThreadForm";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button BtnThread1;
private System.Windows.Forms.Button BtnThread2;
private System.Windows.Forms.Button BtnThread3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Panel panel;
private System.ComponentModel.BackgroundWorker ThreadWorker1;
private System.ComponentModel.BackgroundWorker ThreadWorker2;
private System.ComponentModel.BackgroundWorker ThreadWorker3;
}



public partial class MyBackgroundThreadForm : Form
{
const int WaitTimeInMS = 100;
private Random rnd = new Random();

public MyBackgroundThreadForm()
{
InitializeComponent();
}

private void BtnThread1_Click(object sender, EventArgs e)
{
if (BtnThread1.Text == "Start Thread 1")
{
ThreadWorker1.RunWorkerAsync();
BtnThread1.Text = "StopThread 1";
}
else
{
ThreadWorker1.CancelAsync();
BtnThread1.Text = "Start Thread 1";
}
}

private void BtnThread2_Click(object sender, EventArgs e)
{
if (BtnThread2.Text == "Start Thread 2")
{
ThreadWorker2.RunWorkerAsync();
BtnThread2.Text = "StopThread 2";
}
else
{
ThreadWorker2.CancelAsync();
BtnThread2.Text = "Start Thread 2";
}
}

private void BtnThread3_Click(object sender, EventArgs e)
{
if (BtnThread3.Text == "Start Thread 3")
{
ThreadWorker3.RunWorkerAsync();
BtnThread3.Text = "StopThread 2";
}
else
{
ThreadWorker3.CancelAsync();
BtnThread3.Text = "Start Thread 3";
}
}

private void ThreadWorker1_DoWork(object sender, DoWorkEventArgs e)
{
System.Threading.Thread myThread =
System.Threading.Thread.CurrentThread;

try
{
while (true)
{
if (ThreadWorker1.CancellationPending)
return;

System.Threading.Thread.Sleep(WaitTimeInMS);

using (Graphics g = panel.CreateGraphics())
{
using (Pen pen = new Pen(Color.Red, 5))
{
g.DrawRectangle(pen,
rnd.Next(panel.Width), //x position
rnd.Next(panel.Height), //y position
20, 20); //width, height
}
}
}
}
catch (Exception)
{}
}

private void ThreadWorker2_DoWork(object sender, DoWorkEventArgs e)
{
try
{
while (true)
{
if (ThreadWorker2.CancellationPending)
return;

System.Threading.Thread.Sleep(WaitTimeInMS);
using (Graphics g = panel.CreateGraphics())
{
using (Pen pen = new Pen(Color.Green, 5))
{
g.DrawEllipse(pen,
rnd.Next(panel.Width), //x position
rnd.Next(panel.Height), //y position
20, 20); //width, height
}
}
}
}
catch (Exception)
{}
}

private void ThreadWorker3_DoWork(object sender, DoWorkEventArgs e)
{
try
{
while (true)
{
if (ThreadWorker3.CancellationPending)
return;

System.Threading.Thread.Sleep(WaitTimeInMS);
using (Graphics g = panel.CreateGraphics())
{
using (Pen pen = new Pen(Color.Blue, 5))
{
g.DrawRectangle(pen,
rnd.Next(panel.Width), //x position
rnd.Next(panel.Height), //y position
25, 20); //width, height
}
}
}
}
catch (Exception)
{}
}
}
 
M

Mel Weaver

uses the tag property of the buttons to determine which button is pressed
and passes it to the do work. Of course I didn't enter any error checking.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyBackgroundThreadForm());
}
}


partial class MyBackgroundThreadForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.BtnThread1 = new System.Windows.Forms.Button();
this.BtnThread2 = new System.Windows.Forms.Button();
this.BtnThread3 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.panel = new System.Windows.Forms.Panel();
this.ThreadWorker1 = new
System.ComponentModel.BackgroundWorker();
this.ThreadWorker2 = new
System.ComponentModel.BackgroundWorker();
this.ThreadWorker3 = new
System.ComponentModel.BackgroundWorker();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(310, 13);
this.label1.TabIndex = 0;
this.label1.Text = "This example starts 3 different threads that
sleep for 100ms each";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 22);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(365, 13);
this.label2.TabIndex = 1;
this.label2.Text = "and during each iteration they create output
in the area at a random location";
//
// BtnThread1
//
this.BtnThread1.Location = new System.Drawing.Point(12, 78);
this.BtnThread1.Name = "BtnThread1";
this.BtnThread1.Size = new System.Drawing.Size(87, 23);
this.BtnThread1.TabIndex = 2;
this.BtnThread1.Tag = "1";
this.BtnThread1.Text = "Start Thread 1";
this.BtnThread1.UseVisualStyleBackColor = true;
this.BtnThread1.Click += new
System.EventHandler(this.BtnThread_Click);
//
// BtnThread2
//
this.BtnThread2.Location = new System.Drawing.Point(127, 78);
this.BtnThread2.Name = "BtnThread2";
this.BtnThread2.Size = new System.Drawing.Size(87, 23);
this.BtnThread2.TabIndex = 3;
this.BtnThread2.Tag = "2";
this.BtnThread2.Text = "Start Thread 2";
this.BtnThread2.UseVisualStyleBackColor = true;
this.BtnThread2.Click += new
System.EventHandler(this.BtnThread_Click);
//
// BtnThread3
//
this.BtnThread3.Location = new System.Drawing.Point(239, 78);
this.BtnThread3.Name = "BtnThread3";
this.BtnThread3.Size = new System.Drawing.Size(83, 23);
this.BtnThread3.TabIndex = 4;
this.BtnThread3.Tag = "3";
this.BtnThread3.Text = "Start Thread 3";
this.BtnThread3.UseVisualStyleBackColor = true;
this.BtnThread3.Click += new
System.EventHandler(this.BtnThread_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(9, 115);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 13);
this.label3.TabIndex = 5;
this.label3.Text = "Output from Threads";
//
// panel
//
this.panel.BackColor = System.Drawing.Color.White;
this.panel.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.panel.Location = new System.Drawing.Point(12, 131);
this.panel.Name = "panel";
this.panel.Size = new System.Drawing.Size(559, 360);
this.panel.TabIndex = 6;
//
// ThreadWorker1
//
this.ThreadWorker1.WorkerSupportsCancellation = true;
this.ThreadWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.Thread_DoWork);
//
// ThreadWorker2
//
this.ThreadWorker2.WorkerSupportsCancellation = true;
this.ThreadWorker2.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.Thread_DoWork);
//
// ThreadWorker3
//
this.ThreadWorker3.WorkerSupportsCancellation = true;
this.ThreadWorker3.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.Thread_DoWork);
//
// MyBackgroundThreadForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(596, 503);
this.Controls.Add(this.panel);
this.Controls.Add(this.label3);
this.Controls.Add(this.BtnThread3);
this.Controls.Add(this.BtnThread2);
this.Controls.Add(this.BtnThread1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "MyBackgroundThreadForm";
this.Text = "MyBackgroundThreadForm";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button BtnThread1;
private System.Windows.Forms.Button BtnThread2;
private System.Windows.Forms.Button BtnThread3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Panel panel;
private System.ComponentModel.BackgroundWorker ThreadWorker1;
private System.ComponentModel.BackgroundWorker ThreadWorker2;
private System.ComponentModel.BackgroundWorker ThreadWorker3;
}



public partial class MyBackgroundThreadForm : Form
{
const int WaitTimeInMS = 100;
private Random rnd = new Random();
List<BackgroundWorker> threads = new List<BackgroundWorker>();


public MyBackgroundThreadForm()
{
InitializeComponent();
threads.Add(ThreadWorker1);
threads.Add(ThreadWorker2);
threads.Add(ThreadWorker3);
}


private void BtnThread_Click(object sender, EventArgs e)
{
Button btn = sender as Button;

if (btn != null)
{
int tag;
Int32.TryParse(btn.Tag.ToString(), out tag);
bool startThread = btn.Text.StartsWith("Start");

btn.Text = startThread ? "Stop Thread " : "Start Thread " +
btn.Tag.ToString();

BackgroundWorker thread =
(BackgroundWorker)threads[tag -1 ];

if (startThread)
{
thread.RunWorkerAsync(tag);
}
else
thread.CancelAsync();

}

}

private void Thread_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker workerThread = sender as BackgroundWorker;
int parameter;
Int32.TryParse(e.Argument.ToString(), out parameter);

if (workerThread != null)
{
while (true)
{
if (workerThread.CancellationPending)
{
return;
}

System.Threading.Thread.Sleep(WaitTimeInMS);

using (Graphics g = panel.CreateGraphics())
{
using (Pen pen = new Pen(parameter == 1?
Color.Red:parameter == 2? Color.Green: Color.Blue, 5))
{
g.DrawRectangle(pen,
rnd.Next(panel.Width), //x
position
rnd.Next(panel.Height), //y
position
20, 20); //width,
height
}
}

}

}
}
}



Tony Johansson said:
Hi!

Here is a complete working program that is using
BackgroundWorker(threads).
When button 1 is pushed red square is being drawn on the panel.
If you push again you stop drawing.
It's the same principle with all three buttons.

Button 1 is drawing red square
Button 2 is drawing green circles
Button 3 is drawing blue rectangle

I find my program to long. is it possible to make it shorter by using some
alternative or perhaps better solution ?
What bother me is that each button is toggles between start Thread
respectivily stop Thread.
Can this be done in another way ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyBackgroundThreadForm());
}
}

partial class MyBackgroundThreadForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.BtnThread1 = new System.Windows.Forms.Button();
this.BtnThread2 = new System.Windows.Forms.Button();
this.BtnThread3 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.panel = new System.Windows.Forms.Panel();
this.ThreadWorker1 = new System.ComponentModel.BackgroundWorker();
this.ThreadWorker2 = new System.ComponentModel.BackgroundWorker();
this.ThreadWorker3 = new System.ComponentModel.BackgroundWorker();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(310, 13);
this.label1.TabIndex = 0;
this.label1.Text = "This example starts 3 different threads that
sleep for 100ms each";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 22);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(365, 13);
this.label2.TabIndex = 1;
this.label2.Text = "and during each iteration they create output
in the area at a random location";
//
// BtnThread1
//
this.BtnThread1.Location = new System.Drawing.Point(12, 78);
this.BtnThread1.Name = "BtnThread1";
this.BtnThread1.Size = new System.Drawing.Size(87, 23);
this.BtnThread1.TabIndex = 2;
this.BtnThread1.Text = "Start Thread 1";
this.BtnThread1.UseVisualStyleBackColor = true;
this.BtnThread1.Click += new
System.EventHandler(this.BtnThread1_Click);
//
// BtnThread2
//
this.BtnThread2.Location = new System.Drawing.Point(127, 78);
this.BtnThread2.Name = "BtnThread2";
this.BtnThread2.Size = new System.Drawing.Size(87, 23);
this.BtnThread2.TabIndex = 3;
this.BtnThread2.Text = "Start Thread 2";
this.BtnThread2.UseVisualStyleBackColor = true;
this.BtnThread2.Click += new
System.EventHandler(this.BtnThread2_Click);
//
// BtnThread3
//
this.BtnThread3.Location = new System.Drawing.Point(239, 78);
this.BtnThread3.Name = "BtnThread3";
this.BtnThread3.Size = new System.Drawing.Size(83, 23);
this.BtnThread3.TabIndex = 4;
this.BtnThread3.Text = "Start Thread 3";
this.BtnThread3.UseVisualStyleBackColor = true;
this.BtnThread3.Click += new
System.EventHandler(this.BtnThread3_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(9, 115);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 13);
this.label3.TabIndex = 5;
this.label3.Text = "Output from Threads";
//
// panel
//
this.panel.BackColor = System.Drawing.Color.White;
this.panel.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.panel.Location = new System.Drawing.Point(12, 131);
this.panel.Name = "panel";
this.panel.Size = new System.Drawing.Size(559, 360);
this.panel.TabIndex = 6;
//
// ThreadWorker1
//
this.ThreadWorker1.WorkerSupportsCancellation = true;
this.ThreadWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.ThreadWorker1_DoWork);
//
// ThreadWorker2
//
this.ThreadWorker2.WorkerSupportsCancellation = true;
this.ThreadWorker2.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.ThreadWorker2_DoWork);
//
// ThreadWorker3
//
this.ThreadWorker3.WorkerSupportsCancellation = true;
this.ThreadWorker3.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.ThreadWorker3_DoWork);
//
// MyBackgroundThreadForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(596, 503);
this.Controls.Add(this.panel);
this.Controls.Add(this.label3);
this.Controls.Add(this.BtnThread3);
this.Controls.Add(this.BtnThread2);
this.Controls.Add(this.BtnThread1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "MyBackgroundThreadForm";
this.Text = "MyBackgroundThreadForm";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button BtnThread1;
private System.Windows.Forms.Button BtnThread2;
private System.Windows.Forms.Button BtnThread3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Panel panel;
private System.ComponentModel.BackgroundWorker ThreadWorker1;
private System.ComponentModel.BackgroundWorker ThreadWorker2;
private System.ComponentModel.BackgroundWorker ThreadWorker3;
}



public partial class MyBackgroundThreadForm : Form
{
const int WaitTimeInMS = 100;
private Random rnd = new Random();

public MyBackgroundThreadForm()
{
InitializeComponent();
}

private void BtnThread1_Click(object sender, EventArgs e)
{
if (BtnThread1.Text == "Start Thread 1")
{
ThreadWorker1.RunWorkerAsync();
BtnThread1.Text = "StopThread 1";
}
else
{
ThreadWorker1.CancelAsync();
BtnThread1.Text = "Start Thread 1";
}
}

private void BtnThread2_Click(object sender, EventArgs e)
{
if (BtnThread2.Text == "Start Thread 2")
{
ThreadWorker2.RunWorkerAsync();
BtnThread2.Text = "StopThread 2";
}
else
{
ThreadWorker2.CancelAsync();
BtnThread2.Text = "Start Thread 2";
}
}

private void BtnThread3_Click(object sender, EventArgs e)
{
if (BtnThread3.Text == "Start Thread 3")
{
ThreadWorker3.RunWorkerAsync();
BtnThread3.Text = "StopThread 2";
}
else
{
ThreadWorker3.CancelAsync();
BtnThread3.Text = "Start Thread 3";
}
}

private void ThreadWorker1_DoWork(object sender, DoWorkEventArgs e)
{
System.Threading.Thread myThread =
System.Threading.Thread.CurrentThread;

try
{
while (true)
{
if (ThreadWorker1.CancellationPending)
return;

System.Threading.Thread.Sleep(WaitTimeInMS);

using (Graphics g = panel.CreateGraphics())
{
using (Pen pen = new Pen(Color.Red, 5))
{
g.DrawRectangle(pen,
rnd.Next(panel.Width), //x position
rnd.Next(panel.Height), //y position
20, 20); //width, height
}
}
}
}
catch (Exception)
{}
}

private void ThreadWorker2_DoWork(object sender, DoWorkEventArgs e)
{
try
{
while (true)
{
if (ThreadWorker2.CancellationPending)
return;

System.Threading.Thread.Sleep(WaitTimeInMS);
using (Graphics g = panel.CreateGraphics())
{
using (Pen pen = new Pen(Color.Green, 5))
{
g.DrawEllipse(pen,
rnd.Next(panel.Width), //x position
rnd.Next(panel.Height), //y position
20, 20); //width, height
}
}
}
}
catch (Exception)
{}
}

private void ThreadWorker3_DoWork(object sender, DoWorkEventArgs e)
{
try
{
while (true)
{
if (ThreadWorker3.CancellationPending)
return;

System.Threading.Thread.Sleep(WaitTimeInMS);
using (Graphics g = panel.CreateGraphics())
{
using (Pen pen = new Pen(Color.Blue, 5))
{
g.DrawRectangle(pen,
rnd.Next(panel.Width), //x position
rnd.Next(panel.Height), //y position
25, 20); //width, height
}
}
}
}
catch (Exception)
{}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top