Avoid using Task.Wait or Task.Result while calling async code as this would block on calling the async code. Any code that blocks on calling async code could cause deadlocks. Example:
public static class DeadlockAsyncTaskWait
{
private static async Task DelayAsync()
{
await Task.Delay(1000);
}
// This method causes a deadlock when called in a GUI or ASP.NET context.
// Will work just fine when calling from a console app!
public static void Test()
{
// Start the delay.
var delayTask = DelayAsync();
// Wait for the delay to complete.
delayTask.Wait();
}
}
Moral of the story: Go async all the way and use async await. That's just the nature of asynchronous coding
Reference : https://msdn.microsoft.com/en-us/magazine/jj991977.aspx
No comments:
Post a Comment