I
Ian Williamson
I wanted to keep this one alive as it seems to have
gotten lost over the weekend...
....
Thanks again for responding Chris.
I have worked through many samples which show updating
various objects using a thread outside the UI. What they
all fail to do however is show how to process "something"
within the UI thread while the background thread is
running.
Whenever I try to perform something in the UI after I
have started my background thread, it completely takes
over and the background thread fails to get it's time-
slice.
I am including some sample code to highlight my problem.
I am sure my newbness is causing me to overlook something
obvious.
----
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.ComponentModel;
namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label
TextLabel;
private EventHandler autoSaveDelegate;
private System.Windows.Forms.Button
button1;
private System.Windows.Forms.Button
button2;
private System.Windows.Forms.Label label1;
bool autoSaveComplete = false;
protected override void Dispose( bool
disposing )
{
base.Dispose( disposing );
}
static void Main()
{
Application.Run(new Form1());
}
public Form1()
{
InitializeComponent();
autoSaveDelegate = new
EventHandler(UpdateTextLabel);
}
private void button1_Click(object sender,
System.EventArgs e)
{
this.TextLabel.Text = "Updating";
new Thread(new ThreadStart
(StatusFeedback)).Start();
this.autoSaveComplete = true;
// the call to this method will
cause the new thread to wait until this UI method has
completed
this.PrintNumbers();
}
private void PrintNumbers()
{
for (int i = 0; i < 1000; i++)
{
label1.Text = i.ToString
();
label1.Refresh();
Thread.Sleep(5);
}
}
private void button2_Click(object sender,
System.EventArgs e)
{
this.autoSaveComplete = false;
}
private void StatusFeedback()
{
while (this.autoSaveComplete)
{
this.Invoke
(autoSaveDelegate);
Thread.Sleep(250);
}
}
private void UpdateTextLabel(object
sender,EventArgs e)
{
this.TextLabel.Text =
TextLabel.Text + ".";
this.TextLabel.Refresh();
}
private void InitializeComponent()
{
this.TextLabel = new
System.Windows.Forms.Label();
this.button1 = new
System.Windows.Forms.Button();
this.button2 = new
System.Windows.Forms.Button();
this.label1 = new
System.Windows.Forms.Label();
this.SuspendLayout();
//
// TextLabel
//
this.TextLabel.BackColor =
System.Drawing.SystemColors.ActiveCaptionText;
this.TextLabel.Location = new
System.Drawing.Point(48, 64);
this.TextLabel.Name = "TextLabel";
this.TextLabel.Size = new
System.Drawing.Size(176, 16);
this.TextLabel.TabIndex = 0;
//
// button1
//
this.button1.Location = new
System.Drawing.Point(48, 104);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new
System.Drawing.Point(152, 104);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "Stop";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.Location = new
System.Drawing.Point(112, 24);
this.label1.Name = "label1";
this.label1.Size = new
System.Drawing.Size(40, 23);
this.label1.TabIndex = 3;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new
System.Drawing.Size(5, 13);
this.ClientSize = new
System.Drawing.Size(292, 266);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.TextLabel);
this.Name = "Form1";
this.ResumeLayout(false);
}
}
}
Cheers, Ian
gotten lost over the weekend...
....
Thanks again for responding Chris.
I have worked through many samples which show updating
various objects using a thread outside the UI. What they
all fail to do however is show how to process "something"
within the UI thread while the background thread is
running.
Whenever I try to perform something in the UI after I
have started my background thread, it completely takes
over and the background thread fails to get it's time-
slice.
I am including some sample code to highlight my problem.
I am sure my newbness is causing me to overlook something
obvious.
----
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.ComponentModel;
namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label
TextLabel;
private EventHandler autoSaveDelegate;
private System.Windows.Forms.Button
button1;
private System.Windows.Forms.Button
button2;
private System.Windows.Forms.Label label1;
bool autoSaveComplete = false;
protected override void Dispose( bool
disposing )
{
base.Dispose( disposing );
}
static void Main()
{
Application.Run(new Form1());
}
public Form1()
{
InitializeComponent();
autoSaveDelegate = new
EventHandler(UpdateTextLabel);
}
private void button1_Click(object sender,
System.EventArgs e)
{
this.TextLabel.Text = "Updating";
new Thread(new ThreadStart
(StatusFeedback)).Start();
this.autoSaveComplete = true;
// the call to this method will
cause the new thread to wait until this UI method has
completed
this.PrintNumbers();
}
private void PrintNumbers()
{
for (int i = 0; i < 1000; i++)
{
label1.Text = i.ToString
();
label1.Refresh();
Thread.Sleep(5);
}
}
private void button2_Click(object sender,
System.EventArgs e)
{
this.autoSaveComplete = false;
}
private void StatusFeedback()
{
while (this.autoSaveComplete)
{
this.Invoke
(autoSaveDelegate);
Thread.Sleep(250);
}
}
private void UpdateTextLabel(object
sender,EventArgs e)
{
this.TextLabel.Text =
TextLabel.Text + ".";
this.TextLabel.Refresh();
}
private void InitializeComponent()
{
this.TextLabel = new
System.Windows.Forms.Label();
this.button1 = new
System.Windows.Forms.Button();
this.button2 = new
System.Windows.Forms.Button();
this.label1 = new
System.Windows.Forms.Label();
this.SuspendLayout();
//
// TextLabel
//
this.TextLabel.BackColor =
System.Drawing.SystemColors.ActiveCaptionText;
this.TextLabel.Location = new
System.Drawing.Point(48, 64);
this.TextLabel.Name = "TextLabel";
this.TextLabel.Size = new
System.Drawing.Size(176, 16);
this.TextLabel.TabIndex = 0;
//
// button1
//
this.button1.Location = new
System.Drawing.Point(48, 104);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new
System.Drawing.Point(152, 104);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "Stop";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.Location = new
System.Drawing.Point(112, 24);
this.label1.Name = "label1";
this.label1.Size = new
System.Drawing.Size(40, 23);
this.label1.TabIndex = 3;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new
System.Drawing.Size(5, 13);
this.ClientSize = new
System.Drawing.Size(292, 266);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.TextLabel);
this.Name = "Form1";
this.ResumeLayout(false);
}
}
}
Cheers, Ian
Application.Run). If any other-----Original Message-----
Basically any control at all must only be affected from the primary thread
(the one created with Main and continued in