Reputation: 767
I don´t really understand the Invoking. I get that what it´s doing is that it tells that current action to be run on the thread it needs to be ran on (UI Thread). But what i don´t get is why you can do so many different versions. for example:
this.BeginInvoke(new MethodInvoker(delegate { currping.Text = Status; }));
this.BeginInvoke(new Action(() => { currping.Text = Status; }));
I don´t understand if there is a difference. They will both do the same thing from what i can grasp.
Am i missing something here?
Upvotes: 0
Views: 87
Reputation: 169008
Action
was added in the .NET 3.5 framework as a general delegate that takes no arguments and returns nothing, while MethodInvoker
has existed since 1.1, so you'll typically only see older code using MethodInvoker
. The two delegate types have the same signature.
BeginInvoke()
actually takes a Delegate
object, so any delegate that takes no arguments can be used with that overload. (There is a (Delegate, object[])
overload for delegates that need arguments, though I'd be inclined to use a no-argument closure instead.)
Because Delegate
is a general type, you must specify in some way what type of delegate you are providing because the compiler cannot otherwise infer it (see below). This is shorter than the new Action
alternative, but only by a few characters and it isn't really any more or less clear:
this.BeginInvoke((Action)(() => { ... }));
You have to specify a delegate type here because the compiler doesn't know which delegate type you want. For example:
Action foo = () => { };
This works because the compiler can infer based on the type of foo
that the delegate type should be Action
.
// All of these examples cause CS1660.
Delegate foo1 = () => { };
object foo2 = () => { };
var foo3 = () => { };
These are compile-time errors because there are many delegate types that match the signature "takes no arguments and returns void
" and the compiler doesn't pretend to know which you wanted. (And in the object
and var
cases, you may also have meant an Expression<>
object, which isn't a delegate at all. Nevertheless, you'd get the same error if you used delegate () {}
instead of a lambda expression.)
Upvotes: 3
Reputation: 127553
There are two pieces to this the new Something(
part and the part inside the inner most parenthesis.
Both MethodInvoker
and Action
are Delegates, you need to think of them like "Function pattern" definitions. What both of those two "patterns" represent is "a function that accepts no arguments and returns void"
namespace System.Windows.Forms
{
public delegate void MethodInvoker();
}
namespace System
{
public delegate void Action()
}
So they both represent the exact same "pattern" it is just defined in two places for coding convenience. For example it is more obvious that you are planning on using the delegate to invoke a method for a Winforms Form using MethodInvoker
than using Action
.
the 2nd part is the stuff inside the parenthesis. Both of those two definitions mean the same thing, however the pattern () => { ... }
is a Lambada Expression and was not added till visual studio 2008 or newer. They both mean is just "I am defining a function here inline", this creates what is called an Anonymous Function because it is a function with no name. You also could pass a function name in instead
public void Foo()
{
this.BeginInvoke(new Action(Bar)); //BeginInvoke will call the function Bar()
}
private void Bar()
{
}
Upvotes: 1