Asynchronous method never returns

Possible duplicate:
WinRT: Loading Static Data Using GetFileFromApplicationUriAsync ()

The following code is called in my application, 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 is the expected GetFileFromApplicationUriAsync() method never returned?

+4
source share
1 answer

When you (synchronously) block asynchronous code, you are faced with a deadlock problem .

Follow these guidelines:

  • Use ConfigureAwait(false) , when possible, in your library methods (e.g. Load ).
  • Use async to the end; do not block async code.

In your case, it looks like Load can be called part of the launch. This is a bit tricky to do asynchronously (since constructors cannot be async ). However, you should be able to make it work using asynchronous lazy initialization .

+8
source

Source: https://habr.com/ru/post/1433850/


All Articles