Skip to main content

Posts

Showing posts from April, 2009

MethodInvoker: Invoking a control from a worker thread

Invoking a control from a worker thread is common when you're doing multithreading in a windows form. The usual pattern for this goes something like: private delegate void PrettyMuchUselessDelegate(string text); private void WorkerMethod() {   string result;   // perform some excruciating calculations here to fill the variable 'result'   UpdateUI(result); } private void UpdateUI(string text) {   if(lblResult.InvokeRequired) {     lblResult.Invoke(new PrettyMuchUselessDelegate(UpdateUI),new object[] { text }));   } else {     lblResult.Text = text;   } } Lots of code to do one simple thing: lblResult.Text = text . But by using tools available to us in the framework and the C# compiler, we can compress this down to: private void WorkerMethod() {   string result;   // perform some excruciating calculations here to fill the variable 'result'   lblResult.Invoke(new MethodInvoker(delegate { lblResult.Text = result; })); } Because we know WorkerMethod is running on a work