lysergic-acid lysergic-acid - 1 year ago 105
C# Question

Correct way to get the CoreDispatcher in a Windows Store app

I'm building a Windows Store app, and I have some code that needs to be posted to the UI thread.

For that, i'd like to retrieve the CoreDispatcher and use it to post the code.

It seems that there are a few ways to do so:

// First way

// Second way

I wonder which one is correct? or if both are equivalent?

Answer Source

This is the preferred way:

() =>
    // Your UI update code goes here!

The advantage this has is that it gets the main CoreApplicationView and so is always available. More details here.

There are two alternatives which you could use.

First alternative


This gets the active view for the app, but this will give you null, if no views has been activated. More details here.

Second alternative


This solution will not work when it's called from another thread as it returns null instead of the UI Dispatcher. More details here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download