3

I'm doing simple GUI updates on a timer. Which method is better to use if i am updating a single control? MethodInvoker like this:

this.Invoke((MethodInvoker)delegate
{
  systemMode.Text = systemMode.ToString();
});

or create a control invoke like this:

public void UpdateSystemMode()
{
    if (systemMode.InvokeRequired)
    {
         UpdateSystemMode.Invoke(new
             UpdateSystemModeDelegate(UpdateSystemMode));
    }
    else
    {
        systemMode.UpdateSystemMode();
    }  
}

Obviously, the method invoker has less code up front, but which one is best practice?

Jason
  • 1,882
  • 6
  • 26
  • 37

1 Answers1

2

UpdateSystemMode.Invoke(new UpdateSystemModeDelegate(UpdateSystemMode));

and

this.Invoke((MethodInvoker)delegate
{
  systemMode.Text = systemMode.ToString();
});

is absolutely same as well as

this.Invoke((Action)(()=> systemMode.Text = systemMode.ToString()));

right way:

public void UpdateSystemMode()
{
    if (this.InvokeRequired)
         this.BeginInvoke((Action)UpdateSystemMode);
    else
        systemMode.UpdateSystemMode(); 
}
Nagg
  • 6,040
  • 3
  • 31
  • 39
  • Thanks for the response. Your last line needed the first ';' removed, though. – Jason Jul 25 '11 at 13:00
  • Beware that `BeginInvoke` has different semantics than `Invoke`! And `Control.Invoke` is supposedly faster if you use the `MethodInvoker` delegate type rather than `Action`. – Sven Jul 25 '11 at 13:01
  • @Sven, what is the difference between Action and MethodInvoker for Invoke method? O_o they both are parameterless and have void as return type. – Nagg Jul 25 '11 at 13:05
  • And yes, I know about difference between BeginInvoke and Invoke - BeginInvoke is less rough for perfomance :) – Nagg Jul 25 '11 at 13:06
  • @Nagg - Are you saying the code with the BeginInvoke is the correct way to do it? As i understand it, if i am going to update a bunch of GUI elements at once, I should just use the MethodInvoker instead. Is this true? The reason i ask is that i am running a very fast paced program and i need to keep performance optimal. I will have about 30 GUI elements that need updating. A chunk of them happens in one call, the rest may be piecemeal. – Jason Jul 25 '11 at 13:08
  • @Nagg: on paper they're the same, yeah, but the docs for `Control.Invoke` clearly state "A call to an EventHandler or MethodInvoker delegate will be faster than a call to another type of delegate." I guess there must be some internal optimizations with that particularly delegate type in mind. – Sven Jul 25 '11 at 13:50
  • @Jason, take a look at http://stackoverflow.com/questions/229554/whats-the-difference-between-invoke-and-begininvoke – Nagg Jul 25 '11 at 14:23
  • 1
    @Jason, in short - by using BeginInvoke - your worker-thread will doesnt wait completion of delegated method – Nagg Jul 25 '11 at 14:26
  • @Nagg - Thanks. That was some good info. The calls i'm making to update the GUI are called by another function asynchronously. I'm using a BeginInvoke at that point, so i think i am able to continue on without any blocking. Basically, I have my timer, and on the tick call myDelegate.BeginInvoke, which calls a function that uses the MethodInvoker – Jason Jul 25 '11 at 14:52