K
k.mellor
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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace WindowsApplication2
{
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.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
private void UpdateLabel()
{
if (this.label1.InvokeRequired)
this.label1.Invoke(new MyDelegate(this.UpdateLabel));
else
this.label1.Text += ". ";
}
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.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(40, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(232, 96);
this.label1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 32);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Worker w = new Worker();
w.m_delegate = new MyDelegate(this.UpdateLabel);
Thread t = new Thread(new ThreadStart(w.DoSomething));
t.Start();
Thread.Sleep(20000);
}
}
//
// work class
//
class Worker
{
public MyDelegate m_delegate = null;
public void DoSomething()
{
while (true)
{
Thread.Sleep(1000);
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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace WindowsApplication2
{
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.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
private void UpdateLabel()
{
if (this.label1.InvokeRequired)
this.label1.Invoke(new MyDelegate(this.UpdateLabel));
else
this.label1.Text += ". ";
}
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.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(40, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(232, 96);
this.label1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 32);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Worker w = new Worker();
w.m_delegate = new MyDelegate(this.UpdateLabel);
Thread t = new Thread(new ThreadStart(w.DoSomething));
t.Start();
Thread.Sleep(20000);
}
}
//
// work class
//
class Worker
{
public MyDelegate m_delegate = null;
public void DoSomething()
{
while (true)
{
Thread.Sleep(1000);
m_delegate();
}
}
}
} // end of namespace