how to use Invoke method in a file of extensions/methods? how to use Invoke method in a file of extensions/methods? multithreading multithreading

how to use Invoke method in a file of extensions/methods?


Why not just do this:

label.BeginInvoke( (Action) (() => label.Text = text));

There is no need to create your own delegate. Just use the built-in Action delegate. You should probably create your extension method for the base Control class instead of the Label class. It'll be more reusable.


Change

Invoke((UpdateState)delegate …

to

label.Invoke((UpdateState)delegate …


You forgot to specify the label in your code (when you call the Invoke method):

public static void ShowMessage(this Label label, string text)        {            if (label.InvokeRequired)            {                lablel.Invoke((UpdateState)delegate                {                    label.Text = text;                });            }            else            {                  label.Text = text;            }        }

also, consider using BeginInvoke instead so you won't block the calling thread (if applicable)