Hi,
I hope someone can help. I have written a simple form to demonstrate
my problem/question. The code follows.
The form starts a thread, which using delegates updates a label (Every
second adds another dot to the label). This works great. However,
when I put the GUI thread to sleep (Thread.Sleep), the thread seems to
stall. At first I was expecting dots to still appear, but obviously as
the GUI thread is asleep, they will not. However, the thread never
hits my break points once the GUI thread is asleep. Am I invoking
incorrectly? Any help appreciated.
Kev
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Threadin g;
namespace WindowsApplicat ion2
{
public delegate void MyDelegate();
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.Label label1;
private System.Windows. Forms.Button button1;
private System.Componen tModel.Containe r components = null;
public Form1()
{
InitializeCompo nent();
}
private void UpdateLabel()
{
if (this.label1.In vokeRequired)
this.label1.Inv oke(new MyDelegate(this .UpdateLabel));
else
this.label1.Tex t += ". ";
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.label1 = new System.Windows. Forms.Label();
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// label1
//
this.label1.Fon t = new System.Drawing. Font("Microsoft Sans
Serif", 15.75F, System.Drawing. FontStyle.Bold,
System.Drawing. GraphicsUnit.Po int, ((System.Byte)( 0)));
this.label1.Loc ation = new System.Drawing. Point(40, 32);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(232, 96);
this.label1.Tab Index = 0;
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(80, 168);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(96, 32);
this.button1.Ta bIndex = 1;
this.button1.Te xt = "button1";
this.button1.Cl ick += new
System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.label1) ;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void button1_Click(o bject sender, System.EventArg s e)
{
Worker w = new Worker();
w.m_delegate = new MyDelegate(this .UpdateLabel);
Thread t = new Thread(new ThreadStart(w.D oSomething));
t.Start();
Thread.Sleep(20 000);
}
}
//
// work class
//
class Worker
{
public MyDelegate m_delegate = null;
public void DoSomething()
{
while (true)
{
Thread.Sleep(10 00);
m_delegate();
}
}
}
} // end of namespace
I hope someone can help. I have written a simple form to demonstrate
my problem/question. The code follows.
The form starts a thread, which using delegates updates a label (Every
second adds another dot to the label). This works great. However,
when I put the GUI thread to sleep (Thread.Sleep), the thread seems to
stall. At first I was expecting dots to still appear, but obviously as
the GUI thread is asleep, they will not. However, the thread never
hits my break points once the GUI thread is asleep. Am I invoking
incorrectly? Any help appreciated.
Kev
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Threadin g;
namespace WindowsApplicat ion2
{
public delegate void MyDelegate();
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.Label label1;
private System.Windows. Forms.Button button1;
private System.Componen tModel.Containe r components = null;
public Form1()
{
InitializeCompo nent();
}
private void UpdateLabel()
{
if (this.label1.In vokeRequired)
this.label1.Inv oke(new MyDelegate(this .UpdateLabel));
else
this.label1.Tex t += ". ";
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.label1 = new System.Windows. Forms.Label();
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// label1
//
this.label1.Fon t = new System.Drawing. Font("Microsoft Sans
Serif", 15.75F, System.Drawing. FontStyle.Bold,
System.Drawing. GraphicsUnit.Po int, ((System.Byte)( 0)));
this.label1.Loc ation = new System.Drawing. Point(40, 32);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(232, 96);
this.label1.Tab Index = 0;
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(80, 168);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(96, 32);
this.button1.Ta bIndex = 1;
this.button1.Te xt = "button1";
this.button1.Cl ick += new
System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.label1) ;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void button1_Click(o bject sender, System.EventArg s e)
{
Worker w = new Worker();
w.m_delegate = new MyDelegate(this .UpdateLabel);
Thread t = new Thread(new ThreadStart(w.D oSomething));
t.Start();
Thread.Sleep(20 000);
}
}
//
// work class
//
class Worker
{
public MyDelegate m_delegate = null;
public void DoSomething()
{
while (true)
{
Thread.Sleep(10 00);
m_delegate();
}
}
}
} // end of namespace
Comment