WinRT factory method example using async/await

WinRT factory method example using async/await

Old forum URL: forums.lhotka.net/forums/t/11455.aspx


TSF posted on Thursday, July 05, 2012

Could someone provide an example of where the async/await capabilities fit into an object's public factory methods in WinRT?   From what I have read, I can still do it the old way:

public static void BeginFetchMyList(EventHandler<DataPortalResult<MyInfoList>> callback)
{
  var portal = new DataPortal<MyInfoList>();
  portal.FetchCompleted += callback;
  portal.BeginFetch();
}

But I'd like to take advantage of the async/await features in v4.5.  I did see a recent thread about using async in the DP_XYZ method, but not sure how that fits into the public factory methods.  Thanks.

RockfordLhotka replied on Thursday, July 05, 2012

Yes, the old way continues to work (backward compatibility is important).

The new way is relatively straightforward, and is in the new cslafact snippet.

public static async Task<MyInfoList> FetchMyListAsync()
{
  return await DataPortal.FetchAsync<MyInfoList>();
}

Fwiw, there's a static helper for the old way too, so your existing code could be:

public static void BeginFetchMyList(EventHandler<DataPortalResult<MyInfoList>> callback)
{
  DataPortal.BeginFetch<MyInfoList>(callback);
}

This new async/await support is in the current pre-release, so you can use this now. The async/await support for DataPortal_XYZ will be in the next pre-release (soon).

TSF replied on Thursday, July 05, 2012

Thank you for the example.  So does my DP_XYZ methods also need to include the await keyword when executing a DAL method?  Or at that point, since its already being executed asyncronously can I leave that to be the same as I have always done?

RockfordLhotka replied on Friday, July 06, 2012

In the current pre-release you can not (easily) use async/await concepts in DataPortal_XYZ.

In the next pre-release you will be able to use async/await concepts in DataPortal_XYZ.

Using await on the client-side data portal is entirely independent of using it in the server-side data portal. You can mix and match.

Copyright (c) Marimer LLC