Cannot convert lambda expression to type 'System.Delegate'

ぃ、小莉子 提交于 2019-11-26 13:55:24

问题


Neither of these work:

_uiDispatcher.Invoke(() => { });
_uiDispatcher.Invoke(delegate() { });

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;

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?

Note that this is not a WPF application; I've referenced WindowsBase so I could get access to the Dispatcher class.


回答1:


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?

If your delegate takes no arguments and returns nothing, you can use Action or MethodInvoker. Try this:

_uiDispatcher.Invoke(new Action(() => { }));



回答2:


 this.Dispatcher.Invoke((Action)(() => { textBox1.Text = "Test 123"; }));



回答3:


Unless I've missed something, all you've told us is this is not a WPF application. I don't think the Dispatcher is the correct class to use.

If this is a WinForm app, your UI thread can be accessed via the WindowsFormsSynchronizationContext



来源:https://stackoverflow.com/questions/9549358/cannot-convert-lambda-expression-to-type-system-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!