web analytics
InvokeRequired with anonymous delegates for threading in WinForms Options
codeling
Posted: Friday, March 16, 2018 10:19:26 AM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 938
Points: 3945

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.

Sponsor
Posted: Friday, March 16, 2018 10:19:26 AM
 
Users browsing this topic
Guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.


© 2018 Digcode.com. All rights reserved.