Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async method never returns [duplicate]

Possible Duplicate:
WinRT: Loading static data with GetFileFromApplicationUriAsync()

The following code in my application is called, but it never returns, or throws an exception:

public async Task Load()
{
    ...
    StorageFile file = 
      await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///" + name));
    ...
}

This is how I call the method:

x.Load().Wait();

Why does the awaited method GetFileFromApplicationUriAsync() never return?

like image 337
thumbmunkeys Avatar asked Sep 12 '12 16:09

thumbmunkeys


1 Answers

When you (synchronously) block on asynchronous code, you run into a deadlock problem.

Follow these best practices:

  1. Use ConfigureAwait(false) whenever possible in your library methods (e.g., Load).
  2. Use async all the way down; don't block on async code.

In your case, it sounds like Load may be called as part of startup. This is a bit tricky to do asynchronously (since constructors may not be async). However, you should be able to get it to work by utilizing asynchronous lazy initialization.

like image 172
Stephen Cleary Avatar answered Oct 08 '22 13:10

Stephen Cleary