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:
GetJsonAsyncfinishes await and about to returnjsonStringMainis executingjsonTask.Resultand hold the contextGetJsonAsyncwants to returnjsonStringuntil 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; }