InvokeRequired with anonymous delegates for threading in WinForms Options

codeling Posts: 1054 Points: 4431
Posted: Friday, March 16, 2018 10:19:26 AM

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.

In the following example, 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.
 
public void Add(ListViewItem item)
{
    if (m_ListView.InvokeRequired)
    {
        m_ListView.BeginInvoke(new MethodInvoker(() => Add(item)));
    }
    else
    {
        m_ListView.Items.Add(item);
    }
}
 

For a non lambda version:

public void Add(ListViewItem item)
02.{
    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.

 
Users browsing this topic
Guest