我有一个方法,其中有一些代码执行了 await
操作:
public async Task DoSomething()
{
var x = await ...;
}
我需要在Dispatcher线程上运行该代码。现在,Dispatcher.BeginInvoke()
是可以等待执行的,但我不能将lambda标记为async
,以便从其中运行await
,像这样:
public async Task DoSomething()
{
App.Current.Dispatcher.BeginInvoke(async () =>
{
var x = await ...;
}
);
}
在内部async
中,我遇到了以下错误:
无法将 lambda 表达式转换为类型“System.Delegate”,因为它不是委托类型。
如何在Dispatcher.BeginInvoke()
内使用async
?
Dispatcher.Invoke<Task<int>>(() => DoSomethingWithUIAsync(param));
- noseratio - open to workawait Invoke(...)
起作用时,很明显它正是我们想要的。我只需要看到这个例子就“懂”了。谢谢! - ta.speot.isDebug.Print()
将在var x = await Application.Current.Dispatcher.Invoke ...
之前的同步上下文决定执行位置。如果没有,则会在随机池线程上执行。 - noseratio - open to work