使用匿名方法调用 WPF 调度程序
我刚刚在 C# .Net 4.0 WPF 后台线程中意识到这不起作用(编译器错误):
Dispatcher.Invoke(DispatcherPriority.Normal, delegate()
{
// do stuff to UI
});
从一些示例中我发现它必须像这样进行转换:(Action)delegate()
。然而,在其他示例中,它被转换为其他类,例如 System.Windows.Forms.MethodInvoker。
有人能告诉我上面的例子到底有什么问题吗?我也尝试用其他方法重现它,但它总是在没有强制转换的情况下工作:
delegate void MyAction();
void Method1(MyAction a) {
// do stuff
}
void Method2(Action a) {
// do stuff
}
void Tests()
{
Method1(delegate()
{
// works
});
Method2(delegate()
{
// works
});
Method1(() =>
{
// works
});
Method2(() =>
{
// works
});
Method2(new Action(delegate()
{
// works
}));
new System.Threading.Thread(delegate()
{
// works
}).Start();
}
那么调用调度程序的最佳(最优雅、更少冗余)方法是什么,它有什么特别之处以至于必须强制转换委托?
I just realized in a C# .Net 4.0 WPF background thread that this doesn't work (compiler error):
Dispatcher.Invoke(DispatcherPriority.Normal, delegate()
{
// do stuff to UI
});
From some examples I found out that it had to be casted like this: (Action)delegate()
. However, in other examples it is casted to other classes, e.g. System.Windows.Forms.MethodInvoker
.
Can anybody tell me what exactly is wrong with the example above? I also tried to reproduce it with other methods, but it was always working without casting:
delegate void MyAction();
void Method1(MyAction a) {
// do stuff
}
void Method2(Action a) {
// do stuff
}
void Tests()
{
Method1(delegate()
{
// works
});
Method2(delegate()
{
// works
});
Method1(() =>
{
// works
});
Method2(() =>
{
// works
});
Method2(new Action(delegate()
{
// works
}));
new System.Threading.Thread(delegate()
{
// works
}).Start();
}
So whats the best (most elegant, less redundant) way to invoke the Dispatcher, and whats so special with it that delegates must be casted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想指出比 Svick 的代码示例更简洁的代码示例,毕竟我们都喜欢一个衬垫,不是吗?
I would like to point out even more cleaner code example to Svick's one, after all we all like one liners don't we?
查看
Dispatcher.Invoke()
。它不需要Action
或其他特定的委托类型。它采用Delegate
,它是所有委托类型的共同祖先。但是您不能直接将匿名方法转换为该基类型,只能将其转换为某些特定的委托类型。 (这同样适用于 lambda 和方法组。)为什么需要
Delegate
?因为您可以向其传递带有参数或具有返回值的委托。最干净的方法可能是:
Look at the signature for
Dispatcher.Invoke()
. It doesn't takeAction
or some other specific delegate type. It takesDelegate
, which is the common ancestor of all delegate types. But you can't convert anonymous method to this base type directly, you can convert it only to some specific delegate type. (The same applies to lambdas and method groups.)Why does it take
Delegate
? Because you can pass to it delegates that take parameters or have return values.The cleanest way is probably:
我使用这个:
Dispatcher.Invoke((Action)(() => YourCodeHere()));
I use this one:
Dispatcher.Invoke((Action)(() => YourCodeHere()));
“Delegate”类是抽象类,因此您必须向编译器提供从它派生的类型。任何从 Delegate 派生的类都可以,但 Action 通常是最合适的。
The 'Delegate' class is
abstract
so you have to provide the compiler with a type that is derived from it. Any class derived from Delegate will do but Action is usually the most appropriate one.