Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shorthand for Form.BeginInvoke?

Quite often in my GUI code, I write something like this:

private void SecondTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    if (progressBar1.InvokeRequired)
    {
      progressBar1.BeginInvoke(new ElapsedEventHandler(SecondTimer_Elapsed), new[] {sender, e});
      return;
    }
//Code goes here
}

Of course, this is necessary if the app is multithreaded, as we need to marshall the thread that originally created the control. The thing is, it can be tedious to write the delegate and put the arguments into an array, and it takes up space in the top of every such event handler. Is there an attribute or something along those lines that will replace this code for you? Basically a label that says "if you're on the wrong thread, call me again on the GUI thread with the same args."

like image 501
Carlos Avatar asked Dec 05 '22 01:12

Carlos


1 Answers

I don't know of anything quite like that, but this would probably be a useful extension method for you:

public static class Extensions
{
    public static void Execute(this ISynchronizeInvoke invoker,
                               MethodInvoker action)
    {
        if (invoker.InvokeRequired)
        {
             invoker.BeginInvoke(action);
        }
        else
        {
             action();
        }
    }
}

Now this only works for parameterless delegates, of course... but with lambda expressions that needn't be a problem:

progressBar1.Execute(() => SecondTimer_Elapsed(sender, e));

This has the following advantages:

  • It's simple to write
  • You can't go wrong with the signature (you're not relying on late binding)
  • MethodInvoker is executed slightly more efficiently than other delegates, I believe
  • If you don't have much code to execute, you can write it inline in the lambda expression
like image 118
Jon Skeet Avatar answered Dec 18 '22 05:12

Jon Skeet