CoreWindow::GetCurrentForThread() always NULL

≡放荡痞女 提交于 2019-12-23 13:13:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!