Async Task mechanism and OnNavigatedTo Part 2 #win8dev

Previously i discussed issues i had with #win8dev and #wpdev and using async task mechanism – http://invokeit.wordpress.com/2012/10/15/async-await-and-onnavigatedto-wpdev-win8dev/. With #wpdev you have to consume that mechanism as long as you use async CTP.

So how does one go about doing it in #win8dev #winrt correctly ? We start by marking all methods as async (in this case OnNavigatedTo. If we were firing mutilple calls and possibly making UI calls, #win8dev async implementations is a breeze.

AsyncDataSourceObj aeo = new AsyncDataSourceObj();
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    bErrored = false;
    try
    {
        MyDataType1 mydata1 = await aeo.UseAsyncAwaitStyleCall1();
        MyDataType2 mydata2 = await aeo.UseAsyncAwaitStyleCall2();
        MyDataType3 mydata3 = await aeo.UseAsyncAwaitStyleCall3();
    }
    catch
    {
        bErrored = true;
    }

    if(bErrored)
    {
        await (new MessageDialog("Error")).ShowAsync();
    }
    else
        // process data however you want.
}

above however isn’t a very good implementation as it chains various tasks. you dont want to do this unless the second call depends on the first etc. so if you have distinct calls, one can do this

AsyncDataSourceObj aeo = new AsyncDataSourceObj();
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    bErrored = false;
    try
    {
        List mytasklist = new List();
        mytasklist.Add(aeo.UseAsyncAwaitStyleCall1());
        mytasklist.Add(aeo.UseAsyncAwaitStyleCall2());
        mytasklist.Add(aeo.UseAsyncAwaitStyleCall3());

        await Task.WhenAll(mytasklist);
    }
    catch
    {
        bErrored = true;
    }

    if(bErrored)
    {
        await (new MessageDialog("Error")).ShowAsync();
    }
    else
        // process data however you want.
    // process data however you want.
}

the idea as explained to me by Oren after being told off by Matt is that you leave it to underlying implementation to use whatever it has rather than hacking around with it.

the code as a result is cleaner and works just as well :) What exactly does WhenAll do.. well it asks async task implementation to continue execution when all the tasks in the list are completed. The good part is that its a non blocking call. There’s also WhenAny which continues execution when one of the tasks in the list has completed.

Try and avoid WaitAny or WaitAll as both are blocking calls.

In case of #wpdev it implementation with async ctp was basic and one has to use hacks unforunately. we shall see what Async Targeting Pack brings to #wpdev party in a bit

Just for the record, you might not want to just chuck the exception away.. you should try and log it for future reference as it could be an issue thats just surfacing. have a look at MetroLog on nuget or if you like source, go github

About these ads

3 thoughts on “Async Task mechanism and OnNavigatedTo Part 2 #win8dev

  1. Pingback: Async await and OnNavigatedTo #wpdev | Invoke IT Limited

  2. Pingback: Windows 8 Developer Links – 2012-10-25 | Dan Rigby

  3. Pingback: Windows 8 - Async Task Mechanism and OnNavigatedTo, Part 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s