Async fun with Metro Apps!

Async programming is key today. Making the user wait while you process data is passé ,not acceptable to the modern user and against all usability principles!! But the user does want to be informed when the processing is over and what was the outcome!

So how do we inform the user from asynchronous method?

A simple way is to schedule a callback on the UI thread after completion of the asynchronous tasks.

The main issue here is that the async task will typically be done in the background thread which cannot talk to the UI thread directly.

If you are familiar with WPF or Windows Phone application, we use the “Dispatcher.BeginInvoke” method to resolve this issue.

Control.Dispatcher.BeginInvoke(() = >
{
// Do your UI stuff here
});

While working on a Modern Metro App for the Surface Player we took for granted being able to use similar invokes for communication with the UI and were in for a surprize ! Metro applications don’t support this method (There are many libraries not supported for Metro app..More about that in another blog!)

In a Metro app the parallel method to be used is the “CoreDispatcher.RunAsync” method. It interrupts the UI thread from any other thread and returns the results asynchronously.

var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
     // Do your UI stuff here
});

Key points to note about our new friend! :

  • Use “CoreDispatcherPriority.Low” to make your app more responsive . This will get called when there are no pending input events.
  • CodeDispatcher.RunAsync is asynchronous, there is no synchronous version available.
  • Do not try to reverse dispatch from this method(From UI thread to Background thread) .Instead, use one of the ThreadPool::RunAsync method overloads.

Hope this helps you create your super responsive Metro App!

Quick reminder to double check what all libraries are supported by Metro apps before you start designing your app. For example:-Metro Apps do not allow you to download from Azure Media Services…you can only stream…so tread cautiously!

Until Next time!

Team Cennest

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>