运行 UI-thread 上的代码但在当前线程上调用回调?
Run code on UI-thread but invoke callback on current thread?
我正在编写一个 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()
});
}
我该如何完成?
只需在 await Dispatcher.RunAsync
:
之后调用回调
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.
});
callback();
}
回调将在线程池的工作线程上调用(虽然不一定与 RunOnUIThread
启动的相同,但您可能不需要它)
如果你真的想在同一个线程上调用回调,不幸的是它变得有点混乱,因为工作线程没有同步上下文(让您在特定线程上调用代码)。所以你必须同步调用 Dispatcher.RunAsync
以确保你保持在同一个线程上:
private static void RunOnUIThread(Action callback)
{
//<---- Currently NOT on the UI-thread
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.
}).GetResults();
callback();
}
注意:切勿从 UI 线程调用 GetResults
:这会导致您的应用程序死锁。从工作线程来说,在某些情况下是可以接受的,因为没有同步上下文,所以不会死锁。
我正在编写一个 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()
});
}
我该如何完成?
只需在 await Dispatcher.RunAsync
:
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.
});
callback();
}
回调将在线程池的工作线程上调用(虽然不一定与 RunOnUIThread
启动的相同,但您可能不需要它)
如果你真的想在同一个线程上调用回调,不幸的是它变得有点混乱,因为工作线程没有同步上下文(让您在特定线程上调用代码)。所以你必须同步调用 Dispatcher.RunAsync
以确保你保持在同一个线程上:
private static void RunOnUIThread(Action callback)
{
//<---- Currently NOT on the UI-thread
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.
}).GetResults();
callback();
}
注意:切勿从 UI 线程调用 GetResults
:这会导致您的应用程序死锁。从工作线程来说,在某些情况下是可以接受的,因为没有同步上下文,所以不会死锁。