Which is more correct and why?
Control.BeginInvoke(new Action(DoSomething), null);
private void DoSomething()
{
MessageBox.Show("What a great post");
}
or
Control.BeginInvoke((MethodInvoker) delegate {
MessageBox.Show("What a great post");
});
I kinda feel like I am doing the same thing, so when is the right time to use MethodInvoker
vs Action
, or even writing a lambda expression?
EDIT: I know that there isn't really much of a difference between writing a lambda vs Action
, but MethodInvoker
seems to be made for a specific purpose. Is it doing anything different?
Both are equally correct, but the documentation for Control.Invoke
states that:
The delegate can be an instance of EventHandler, in which case the sender parameter will contain this control, and the event parameter will contain EventArgs.Empty. The delegate can also be an instance of MethodInvoker, or any other delegate that takes a void parameter list. A call to an EventHandler or MethodInvoker delegate will be faster than a call to another type of delegate.
So MethodInvoker
would be a more efficient choice.