【发布时间】:2019-06-05 04:23:09
【问题描述】:
我正在尝试执行以下操作:
Windows::ApplicationModel::Core::CoreApplication::
MainView->CoreWindow->Dispatcher->
ProcessEvents(Windows::UI::Core::CoreProcessEventsOption::
ProcessOneAndAllPending);
但是以毫秒为单位的超时,例如:等待最多 50 毫秒,直到事件发生。
在 Win32 中我可以做到:
MsgWaitForMultipleObjects(0, null, false, wait,
QS_ALLINPUT);
但不能为 UWP 做到这一点。
或者,有什么方法可以知道是否有任何事件在等待?
所以我可以结合检查事件是否存在与Sleep(1)
【问题讨论】:
-
创建一个计时器,在 50 毫秒后将事件发布到您的调度程序。如果要投票,请使用 ProcessOneIfPresent 或 ProcessAllIfPresent。
-
如何创建定时器?我试过做“Timer=ref new Windows::UI::Xaml::DispatcherTimer();”我收到一个异常“HRESULT:0x8001010E 应用程序调用了一个为不同线程编组的接口。”
-
然后我尝试在 RunAsync 中创建它: if(!Timer) Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(Windows::UI ::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([=]() { if(!Timer) { Timer=ref new Windows::UI::Xaml::DispatcherTimer( ); } }));和同样的崩溃再次
-
使用普通计时器,而不是调度程序计时器。普通计时器将事件发布到调度程序;它不需要在调度程序上运行。