Galin Iliev's blog

Software Architecture & Development

Elegant update winform controls from child thread

I've just came across this interesting solution to update winform controls from child thread

private void button17_Click(object sender, EventArgs e)
{
// Create and start Worker Thread using anonymous delegate.

new Thread((ThreadStart)delegate()
{
for (int i = 0; i < 5; i++)
{
//this.richTextBox1.AppendText("Cross-thread operation will not work.");

// Update UI on the UI thread using another anonymous delegate.
this.BeginInvoke((ThreadStart)delegate()
{
this.richTextBox1.AppendText("Worker updated UI.\n");
});
}
}).Start();
}

Many thanks to William Stacey [MVP]

Pingbacks and trackbacks (181)+

Loading