views:

86

answers:

3

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."

+6  A: 

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
Jon Skeet
This is really rather clever!
Carlos
Nitpicking, but shouldn't the extension method be in a static class?
fletcher
@fletcher: Absolutely. Fixing.
Jon Skeet
A: 

Grab an AOP framework for this. You can create an MethodInterceptionAspect that fires when a function is called. Then you can do the checks, and either toss the function (you have a ref to the function and it's arguments) to the GUI thread, or execute it directly.

Advantage is that you will have to write this code once, and can apply it to every property needed with just an attribute.

Jan Jongboom
+2  A: 

You can tidy this up with extension methods:

// Extension methods.
public static void BeginInvoke(this ISynchronizeInvoke @this, MethodInvoker action) {
    if (@this.InvokeRequired) @this.BeginInvoke(action);
    else action();
}

public static void BeginInvoke<T1, T2>(this ISynchronizeInvoke @this, Action<T1, T2> action, T1 arg1, T2 arg2) {
    if (@this.InvokeRequired) @this.BeginInvoke(action, new object[] { arg1, arg2 });
    else action(arg1, arg2);
}

// Code elsewhere.
progressBar1.BeginInvoke(() => SecondTimer_Elapsed(sender, e));
// Or:
progressBar1.BeginInvoke(SecondTimer_Elapsed, sender, e);

Edit: Jon Skeet is correct in saying that using the MethodInvoker delegate is faster. From the MSDN:

A call to an EventHandler or MethodInvoker delegate will be faster than a call to another type of delegate.

Rich