Hello,
this problem was interesting to me when I was learning C#, and here is my solution :
MainForm.cs
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace Buttons_and_Threading
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
#region- Parallel process methods and variables -
Thread Parallel;
int i;
protected delegate void Refresh_Data(string Text);
void Button_1()
{
for (i = 0; i < 1000000; i++)
{
if(label1.InvokeRequired)
{
Refresh_Data refresh = Refresh_Label_1_Text;
Invoke(refresh,i.ToString());
}
}
}
void Initialize_and_Start_New_Thread()
{
Parallel = new Thread(Button_1);
Parallel.Start();
}
void Stop_Thread()
{
Parallel.Abort();
while(Parallel.ThreadState != ThreadState.Aborted)
{
}
}
void Refresh_Label_1_Text(string Text)
{
label1.Text = Text;
}
void MainFormFormClosing(object sender,
System.Windows.Forms.FormClosingEventArgs e)
{
if(Parallel.ThreadState != ThreadState.Aborted)
{
Parallel.Abort();
while(Parallel.ThreadState != ThreadState.Aborted)
{
}
}
}
#endregion
void Button1Click(object sender, EventArgs e)
{
if(button1.ForeColor == Color.ForestGreen)
{
button1.ForeColor = Color.Red;
Initialize_and_Start_New_Thread();
}
else
{
Stop_Thread();
button1.ForeColor = Color.ForestGreen;
}
}
void Button2Click(object sender, EventArgs e)
{
i=0;
if(label1.InvokeRequired)
{
Refresh_Data refresh = Refresh_Label_1_Text;
Invoke(refresh,i.ToString());
}
else
Refresh_Label_1_Text(i.ToString());
}
}
}
Main.Form.Designer.cs
namespace Buttons_and_Threading
{
partial class MainForm
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.button1.ForeColor = System.Drawing.Color.ForestGreen;
this.button1.Location = new System.Drawing.Point(14, 18);
this.button1.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(264, 38);
this.button1.TabIndex = 0;
this.button1.Text = "Button 1";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.Button1Click);
this.button2.ForeColor = System.Drawing.Color.ForestGreen;
this.button2.Location = new System.Drawing.Point(14, 64);
this.button2.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(264, 38);
this.button2.TabIndex = 1;
this.button2.Text = "Button 2";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.Button2Click);
this.label1.Location = new System.Drawing.Point(20, 127);
this.label1.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(158, 29);
this.label1.TabIndex = 2;
this.label1.Text = "Label 1";
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(292, 171);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(300, 200);
this.MinimizeBox = false;
this.Name = "MainForm";
this.Text = "Buttons and Threading";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainFormFormClosing);
this.ResumeLayout(false);
}
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
}
}
Try to learn how to work with Delegates, codeproject has many articles on that subject.
All the best,
Željko Perić