Skip to content

Instantly share code, notes, and snippets.

@Khubajsn
Created April 18, 2013 17:18
Show Gist options
  • Select an option

  • Save Khubajsn/5414532 to your computer and use it in GitHub Desktop.

Select an option

Save Khubajsn/5414532 to your computer and use it in GitHub Desktop.
Finally found a way to quickly and efficiently manipulate windows form controls from another thread - doing this without invoking the control results in an exception.
using System.Threading;
private void button1_Click(object sender, EventArgs e)
{
testingLabel.Text = "Thread started.";
Thread t = new Thread(doTheMagic);
t.Start();
}
private void doTheMagic()
{
Thread.Sleep(5000);
testingLabel.Invoke((Action)delegate
{
testingLabel.Text = "Works!";
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment