Jason
Jason

Reputation: 2197

MethodInvoker vs Control.Invoke

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?

Upvotes: 4

Views: 6805

Answers (1)

EgorBo
EgorBo

Reputation: 6142

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(); 
}

Upvotes: 3

Related Questions