.NET – ThreadSafe updating controls from a non-gui thread

Thread-safe updating of controls from another thread

https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread

Another method (from BB)

private void buttonOk_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
buttonOk.Enabled = false;
int count = 0;
SaveSettings();
for (int i = 0; i < (int)NoOfMessages; i++)
{
new Thread(new ThreadStart(() =>
{
Stopwatch watch = new Stopwatch();
watch.Start();
var message = PerformWork(.........);
watch.Stop();
var tempCount = ++count;

Invoke(new MethodInvoker(() =>
{
textBoxLog.Text += "Message " + (tempCount) + " done." + Environment.NewLine;
if (tempCount == (int)NoOfMessages)
{
Cursor = Cursors.Default;
buttonOk.Enabled = true;
}
// Console.WriteLine("DONE " + tempCount);
}));
})).Start();
}
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *