I am reading this article about deadlock, and I tried with my own code and it doesn't have deadlock issue. Why is that?
I am expecting:
GetJsonAsync
finishes await and about to returnjsonString
Main
is executingjsonTask.Result
and hold the contextGetJsonAsync
wants to returnjsonString
until context is availabledeadlock will happen
public static void Main(string[] args) { var jsonTask = GetJsonAsync(); int i = jsonTask.Result; Console.WriteLine(jsonTask.Result); } public static async Task<int> GetJsonAsync() { var jsonString = await ReturnIntAsync(); return jsonString; } public static async Task<int> ReturnIntAsync() { int i = 0; await Task.Run(() => i++); return i; }