Skip to main content

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 worker thread, we don't bother checking InvokeRequired. We can directly call Invoke on our control, passing a new delegate of type System.Windows.Forms.MethodInvoker. MethodInvoker is a delegate for a method that takes no parameters and returns void. Instead of creating a separate method to invoke, we pass in an anonymous method that sets the text of the label. Using MethodInvoker and anonymous methods helps make the code simpler and more elegant, and what's more important than that?

Comments

Popular posts from this blog

Smart Coding with Visual Studio

Coding is one of the daily tasks that we developers do. Its like brushing our teeth or taking a shower. But how many of us do it smartly. If we want to spend some of our coding time in other activities like blogging or learning new technologies, then we have to be smart while coding. So the million dollar question is, What do we do to code smartly? Actually, some of us do it, but we hardly recognize them. We do keep track of some typical day to day coding practices and snippets, so that we can pull them out when needed. Or we blog about them, so that if we come across the same problem again, then just pull out the code and implement it again. And in the process we also help others to know about some of our problems and solutions. Another way that Visual Studio provides is through Code Snippets. The Studio provides some really smart coding practices by default to ease our coding woes. We can also write our custom snippets, so that we can use them when necessary. But, we hardly do that.

Microsoft Dynamics CRM 4.0 - Online User Handbook

Microsoft Dynamics CRM has become a buzzword now a days and all companies are betting big on it. Even Microsoft is projecting it as an xRM platform to get into all sort of industries and domains. Many companies are building customized applications on Dynamics CRM to meet the business needs. Here is a link that would help you to learn Microsoft Dynamics CRM 4.0 http://www.redware.com/mscrm/handbook/index.html It is mainly a Handbook for Users that helps them to know the usage of Dynamics CRM.

Creating PDFs with C# using Ghostscript

Portable Document Format (PDF) is a file format from Adobe that enables a document to be distributed on different systems while preserving the layout. It has become a standard for secured and reliable distribution and exchange of electronic documents around the world. It preserves the fonts, images, graphics, and layout of any source document, regardless of the application and platform used to create it, thus making it cross-platform and cross-browser compatible. With the increased use of PDF documents as a universal format for sharing documents and managing the paperless office, it has become a part of the commercial applications to be able to convert documents of different format to PDF. In this article we will discuss how we can use Ghostscript to convert various documents into PDF. Read the complete article here .