Possible Duplicate:
MethodInvoker vs Action for Control.BeginInvoke
ThreadPool.QueueUserWorkItem(x =>
{
//get dataset from web service
BeginInvoke(new Action(() => {
//fill grid
}));
BeginInvoke(new MethodInvoker(() => {
//fill grid
}));
});
开发者_JAVA技巧
In C# 2.0 I was using allot the MethodInvoker to update the UI from a background thread, will it be wise to switch to Action when using under BeginInvoke? Is it faster or safer to use Action?
It really doesn't make a difference, as both are simply delegate types which take no parameters and don't return a value. However, in terms of naming semantics, MethodInvoker
is specific to WinForms and should therefore be limited to that scope. Action
is all-purpose and can be used in any area of the framework.
They are both delegates, and identical to each other in definition (grabbed from MSDN):
public delegate void MethodInvoker()
public delegate void Action()
So at the IL level they should be pretty much identical. So I doubt it matters much which one you use. Action
is more universal and more likely to be understood by more developers, but MethodInvoker
does have a more descriptive name. Go with whichever one feels better to you.
But as davidsoa points out, you can skip them both and just use a lambda directly.
精彩评论