Updating UI controls from worker thread
//Updating UI controls from worker thread
void WorkerThreadMethod()
{
//Using Action delegate
Action actionDelegate = () => label1.Text = "Hello from Worker Thread";
Action actionDelegate1 = delegate() { label1.Text = "Hello from Worker Thread"; };
this.Invoke(actionDelegate);
//Using Func delegate
System.Func<int> funcDelegate = () => { label1.Text = "Hello from Worker Thread"; return 5; };
System.Func<int> funcDelegate1 = delegate() { label1.Text = "Hello from Worker Thread"; return 5; };
this.Invoke(funcDelegate1);
//Using MethodInvoker delegate
MethodInvoker methodInvokerDelegate = delegate() { label1.Text = "Hello from Worker Thread"; };
MethodInvoker methodInvokerDelegate1 = () => { label1.Text = "Hello from Worker Thread"; };
this.Invoke(methodInvokerDelegate);
//WPF
Dispatcher.Invoke(action);
//WinForms
this.Invoke(methodInvokerDelegate);
}
private void workerThreadButton_Click(object sender, EventArgs e)
{
new Thread(WorkerThreadMethod).Start();
}