InvokeRequired with anonymous delegates for threading in WinForms!

Here’s a little cookie from the cookie jar. To quote the legendary Jon Skeet from Threading with Windows Forms:

There are two different ways of invoking a method on the UI thread, one synchronous (Invoke) and one asynchronous (BeginInvoke). They work in much the same way – you specify a delegate and (optionally) some arguments, and a message goes on the queue for the UI thread to process. If you use Invoke, the current thread will block until the delegate has been executed. If you use BeginInvoke, the call will return immediately. If you need to get the return value of a delegate invoked asynchronously, you can use EndInvoke with the IAsyncResult returned by BeginInvoke to wait until the delegate has completed and fetch the return value.

Here’s a simple, uncluttered version you can utilise and reuse – this example just adds a list item to the listview.

public void Add(ListViewItem item)
{
    if (m_ListView.InvokeRequired)
    {
        m_ListView.BeginInvoke(Add(item)));
    }
    else
    {
        m_ListView.Items.Add(item);
    }
}

First we check whether we’re executing on the GUI thread or not (InvokeRequired), then execute a delegate thats parsed into the MethodInvoker calling itself using a lambda expression. This code is VS2008 compatible (.NET 2.0 +).

For a non lambda version:

public void Add(ListViewItem item)
{
    if (m_ListView.InvokeRequired)
    {
        m_ListView.BeginInvoke(new MethodInvoker(delegate
                                {
                                    Add(item);
                                }));
    }
    else
    {
        m_ListView.Items.Add(item);
    }
}

The advantage of using an anonymous delegate is that by design, delegates are able to use local variables and parameters scoped in the containing method. Therefore we didn’t need to create a custom delegate with the signature to pass onto the method.

Related Articles

Comments have been disabled.