I came across some best practices for asynchronous programming using c#\'s async
/await
keywords (I\'m new to c# 5.0).
One of the advices gi
GetDataAsync().Result;
will run when the task returned by GetDataAsync()
completes, in the meantime it blocks the UI threadreturn result.ToString()
) is queued to the UI thread for executionGetDataAsync()
will complete when its queued continuation is runThe deadlock can be broken by provided alternatives to avoid Fact 1 or Fact 2.
var data = await GetDataAsync()
, which allows the UI thread to keep runningvar data = Task.Run(GetDataAsync).Result
, which will post the continuation to the sync context of a threadpool thread. This allows the task returned by GetDataAsync()
to complete. This is explained really well in an article by Stephen Toub, about half way down where he uses the example of DelayAsync()
.