Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 665
Jason Avatar asked Oct 31 '25 16:10

Jason


1 Answers

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(); 
}
like image 133
Nagg Avatar answered Nov 02 '25 08:11

Nagg