无法转换拉姆达前pression键入'System.Delegate' [英] Cannot convert lambda expression to type 'System.Delegate'
问题描述
无论这些工作:
_uiDispatcher.Invoke(() => { });
_uiDispatcher.Invoke(delegate() { });
所有我想做的事就是我的主UI线程调用一个内联方法。所以,我将此称为主线程:
All I want to do is Invoke an inline method on my main UI thread. So I called this on the main thread:
_uiDispatcher = Dispatcher.CurrentDispatcher;
现在我想执行从另一个线程的线程一些code。我该怎么做?我使用了错误的语法?
And now I want to execute some code on that thread from another thread. How do I do it? Am I using the wrong syntax?
请注意,这是的不的WPF应用程序;我引用 WindowsBase
这样我就可以访问该调度
类。
Note that this is not a WPF application; I've referenced WindowsBase
so I could get access to the Dispatcher
class.
推荐答案
的问题是,你没有提供确切的委托类型的要调用。 Dispatcher.Invoke
只是需要代表
。它是一个动作< T>
?如果有,是什么 T
?它是一个 MethodInvoker
? 动作
?什么?
The problem is that you aren't providing the exact type of delegate you want to invoke. Dispatcher.Invoke
just takes a Delegate
. Is it an Action<T>
? If so, what is T
? Is it a MethodInvoker
? Action
? What?
如果您的委托没有参数,没有返回值,可以使用动作
或 MethodInvoker
。试试这个:
If your delegate takes no arguments and returns nothing, you can use Action
or MethodInvoker
. Try this:
_uiDispatcher.Invoke(new Action(() => { }));
这篇关于无法转换拉姆达前pression键入'System.Delegate'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!