WP8 await on async void methods?

Aug 5, 2013 at 1:20 AM
Edited Aug 5, 2013 at 1:25 AM
Is it possible to await the result of the async methods? I tried await Task.Run but it doesn't seem to work.

For example
PhotoCollection pc = new PhotoCollection();
        await Task.Run(() => f.PeopleGetPhotosAsync(p.UserId, options, page, count, (x) =>
        {
            if (x.Error == null)
            {
                pc = x.Result;
            }
            else
            {
                MessageBox.Show(x.Error.Message);
            }
        }));
        return pc;
pc returns empty, but x.Result is valid so my code does not appear to be waiting for the call to complete. Sorry, I am new to async and tasks.
Coordinator
Aug 5, 2013 at 12:48 PM
I think I would do something like this:
var pc = await Task<PhotoCollection>.Run(() =>
  {
    var t = new TaskCompletionSource<PhotoCollection>();

    f.PeopleGetPhotosAsync(p.UserId, options, page, count, r => { 
      if(r.HasError)
        t.TrySetException(r.Error);
      else
        t.TrySetResult(r.Result);
    });

    return t.Task;
  });
I haven't tested this, but it might work.
Aug 6, 2013 at 4:50 PM
Thank you Sam, I will try this later!