CoreWindow::GetCurrentForThread() always NULL
Asked Answered
E

2

5

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?

Estray answered 24/1, 2013 at 8:46 Comment(0)
W
2

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.

Wretched answered 24/1, 2013 at 16:4 Comment(1)
I'm sure I call from the UI thread (from the mouse event). It's still NULL.Raneeraney
S
2

I have used

await CoreApplication.Views.First().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    //your code here
                });
Shifrah answered 5/3, 2017 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.