D
Drakier Dominaeus
This is my first time posting here, so please forgive me if I do anything
incorrectly.
I've been learning C# and working with different things and decided I wanted
to get into Multi-Threading. My problem is that I must not be doing it right
because only some of the stuff works as would be expected. I'll post what
exactly is happening, then I'll post the sample code I'm using that is
giving me the problems. I'm sure its something I've overlooked, or I'm just
doing it completely wrong.
Below is the code for my sample application. It has 2 buttons and a label.
Basically, the thread is just supposed to make the label visible and loop
every 5 seconds. the top button starts the thread going, and the bottom
button calls the Cancel() method which turns off blocking and allows the
thread to exit properly. The problem is that the label sometimes appears to
become visible, but if the form is minimized, or a window is moved over it,
you'll see its really not. Also.. the Form seems to become VERY laggy and
acts as though its processing REALLY hard (although its not as the
taskmanager says its using very little CPU).
You may ask why I'm using Events and that is because what I plan on doing is
making a worker thread that will send periodic updates back to the main
program to progress a status bar. Why I'm using threads and events isn't
important. What is important is that I must be doing something wrong, and I
want to learn how I should do it correctly.
Anyway.. that is my problem.. here is the source...
=============================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace ThreadTest
{
#region TestEventHandler
public delegate void TestEventHandler(object sender, TestEventArgs e);
public class TestEventArgs : EventArgs
{
public bool ShowLabel = false;
public TestEventArgs(bool bShowLabel) { ShowLabel = bShowLabel; }
}
#endregion
#region Test Thread
public class clsThreadTest
{
public event TestEventHandler ShowLabel;
private bool blocked = false;
private Thread TestThread = null;
public clsThreadTest() { }
public void SetLabel(bool bVisible)
{
if (ShowLabel != null)
{
TestEventArgs tea = new TestEventArgs(bVisible);
ShowLabel(this, tea);
}
}
public void Start()
{
if (!blocked)
{
blocked = !blocked;
TestThread = new Thread(new ThreadStart(RunTest));
TestThread.ApartmentState = ApartmentState.MTA;
TestThread.Priority = ThreadPriority.Lowest;
TestThread.Name = "Test Thread";
TestThread.Start();
}
}
public void Cancel()
{
blocked = false;
}
public void RunTest()
{
while (blocked)
{
SetLabel(true);
Thread.Sleep(5000);
}
}
}
#endregion
public class Form1 : System.Windows.Forms.Form
{
private clsThreadTest cThreadTest = new clsThreadTest();
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
cThreadTest.ShowLabel += new
TestEventHandler(cThreadTest_ShowLabel);
}
public void cThreadTest_ShowLabel(object sender, TestEventArgs tea)
{
label1.Visible = tea.ShowLabel;
label1.Refresh();
this.Refresh();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 12F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(42, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(108, 24);
this.label1.TabIndex = 0;
this.label1.Text = "Hello!";
this.label1.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(186, 6);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 18);
this.button1.TabIndex = 1;
this.button1.Text = "Start Thread";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(186, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 18);
this.button2.TabIndex = 2;
this.button2.Text = "Queue Stop";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 45);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1,
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
cThreadTest.Start();
}
private void button2_Click(object sender, System.EventArgs e)
{
cThreadTest.Cancel();
}
}
}
incorrectly.
I've been learning C# and working with different things and decided I wanted
to get into Multi-Threading. My problem is that I must not be doing it right
because only some of the stuff works as would be expected. I'll post what
exactly is happening, then I'll post the sample code I'm using that is
giving me the problems. I'm sure its something I've overlooked, or I'm just
doing it completely wrong.
Below is the code for my sample application. It has 2 buttons and a label.
Basically, the thread is just supposed to make the label visible and loop
every 5 seconds. the top button starts the thread going, and the bottom
button calls the Cancel() method which turns off blocking and allows the
thread to exit properly. The problem is that the label sometimes appears to
become visible, but if the form is minimized, or a window is moved over it,
you'll see its really not. Also.. the Form seems to become VERY laggy and
acts as though its processing REALLY hard (although its not as the
taskmanager says its using very little CPU).
You may ask why I'm using Events and that is because what I plan on doing is
making a worker thread that will send periodic updates back to the main
program to progress a status bar. Why I'm using threads and events isn't
important. What is important is that I must be doing something wrong, and I
want to learn how I should do it correctly.
Anyway.. that is my problem.. here is the source...
=============================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace ThreadTest
{
#region TestEventHandler
public delegate void TestEventHandler(object sender, TestEventArgs e);
public class TestEventArgs : EventArgs
{
public bool ShowLabel = false;
public TestEventArgs(bool bShowLabel) { ShowLabel = bShowLabel; }
}
#endregion
#region Test Thread
public class clsThreadTest
{
public event TestEventHandler ShowLabel;
private bool blocked = false;
private Thread TestThread = null;
public clsThreadTest() { }
public void SetLabel(bool bVisible)
{
if (ShowLabel != null)
{
TestEventArgs tea = new TestEventArgs(bVisible);
ShowLabel(this, tea);
}
}
public void Start()
{
if (!blocked)
{
blocked = !blocked;
TestThread = new Thread(new ThreadStart(RunTest));
TestThread.ApartmentState = ApartmentState.MTA;
TestThread.Priority = ThreadPriority.Lowest;
TestThread.Name = "Test Thread";
TestThread.Start();
}
}
public void Cancel()
{
blocked = false;
}
public void RunTest()
{
while (blocked)
{
SetLabel(true);
Thread.Sleep(5000);
}
}
}
#endregion
public class Form1 : System.Windows.Forms.Form
{
private clsThreadTest cThreadTest = new clsThreadTest();
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
cThreadTest.ShowLabel += new
TestEventHandler(cThreadTest_ShowLabel);
}
public void cThreadTest_ShowLabel(object sender, TestEventArgs tea)
{
label1.Visible = tea.ShowLabel;
label1.Refresh();
this.Refresh();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 12F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(42, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(108, 24);
this.label1.TabIndex = 0;
this.label1.Text = "Hello!";
this.label1.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(186, 6);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 18);
this.button1.TabIndex = 1;
this.button1.Text = "Start Thread";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(186, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 18);
this.button2.TabIndex = 2;
this.button2.Text = "Queue Stop";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 45);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1,
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
cThreadTest.Start();
}
private void button2_Click(object sender, System.EventArgs e)
{
cThreadTest.Cancel();
}
}
}