我正在编写一个Windows 10通用应用程序。我需要在UI线程上运行一些特定的代码,但是一旦该代码完成,我希望在最初调用请求的同一线程上运行一些代码。请参见下面的示例:
private static async void RunOnUIThread(Action callback)
{
//<---- Currently NOT on the UI-thread
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
//Do some UI-code that must be run on the UI thread.
//When this code finishes:
//I want to invoke the callback on the thread that invoked the method RunOnUIThread
//callback() //Run this on the thread that first called RunOnUIThread()
});
}
我该如何实现这个呢?
async void
,请不要这样做!,要么让你的方法返回async Task
,要么不要使用异步方法。 - Scott Chamberlain