UWP Dispatcher用法

Task.Run(async () =>
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
        {
            ContentDialog dialog = new ContentDialog()
            {
                Title = "Test Dispatcher(测试Dispatcher)",
                DefaultButton = ContentDialogButton.Close,
                CloseButtonText = "Close(关闭)"
            };
            await dialog.ShowAsync();
        });
});

根据CoreDispatcherPriority Enum,优先级分为:
High:高优先级。 所有同步请求都会立即调用委托。 异步请求在任何其他请求类型之前排队和处理。
不要在应用程序中使用此优先级。 它是为系统事件保留的。 使用此优先级可能会导致其他消息(包括系统事件)被延迟。
Normal:正常优先。 事件按安排的顺序进行处理。
Low:低优先级。 如果队列中没有更高优先级的事件,则处理该事件。
Idle:最低优先级。 将此优先级用于后台任务。 当窗口的主线程空闲并且队列中没有待处理的输入时,会处理委托。

示例代码

DispatcherDemo.xaml.cs

参考资料

CoreDispatcher Class
CoreDispatcher.RunAsync
CoreDispatcherPriority Enum
Getting the Dispatcher of a Window in UWP

posted @ 2022-02-17 20:40  Lulus  阅读(264)  评论(0编辑  收藏  举报