Home > .NET / CLR / C#, Developer > InvokeRequired with anonymous delegates for threading in WinForms!

InvokeRequired with anonymous delegates for threading in WinForms!

September 12th, 2009 Leave a comment Go to comments

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(new MethodInvoker(() => 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.

{lang: 'en-GB'}
Share

Related posts:

  1. Design Patterns in C# & Java : The Singleton.
  2. Mono 2.0 Released today!
  1. Long Zhen
    September 14th, 2009 at 19:25 | #1

    LOVELY# neat and clean how to get it to work in .NET 2.0?

  2. February 15th, 2011 at 02:03 | #2

    That’s a bit more concise than the callback delegate declarations I was using for this pattern.

    I’m still trying to come up with a more generic way to do this, but short of having a Func parameter with overloads for several input parameters to that function, I don’t see a way to do it cleanly.

  1. No trackbacks yet.