问题
I'm trying to get the CoreDispatcher
in C++ on Windows Phone 8 so that I can submit work items to the UI thread Dispatcher so I can update UI elements on the UI thread. However, when I call CoreWindow::GetCurrentForThread()
, I get NULL
back. In the documentation it states that this is supported on WP8. As long as I'm getting NULL
for the current Window, I can't get the current Dispatcher
from it; does anyone know how to get the current Dispatcher on WP8?
回答1:
CoreWindow::GetForCurrentThread()
is documented as returning:
The
CoreWindow
for the currently active thread.
If you call this function from a thread that does not have a CoreWindow
(like any non-UI thread), then this function will return nullptr
.
Assuming the application has finished initializing and there is a view, you can use the dispatcher from the main view of the application via CoreApplication::MainView. Alternatively, you can pass the Dispatcher^
for the UI thread to the code executing on the non-UI thread so that it has access to it when it needs to invoke back onto the UI thread.
回答2:
I have used
await CoreApplication.Views.First().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
//your code here
});
来源:https://stackoverflow.com/questions/14497147/corewindowgetcurrentforthread-always-null