As we all know to access the Result property of Task in the UI thread and synchronous mode will deadlock.
As theoretical follow code will deadlock but not. Can you please explain why?
// My "library" method.
public static async Task<JObject> GetJsonAsync(Uri uri)
{
using (var client = new HttpClient())
{
var jsonString = await client.GetStringAsync(uri);
return JObject.Parse(jsonString);
}
}
//MVC action
public ActionResult Index()
{
var result = System.Threading.Tasks.Task.Run(async () => await GetJsonAsync(...)).Result; // deadlock is expectation but not :(
...
}
I thought that System.Threading.Tasks.Task.Run(async () => await GetJsonAsync(...)).Result
is simialar to GetJsonAsync(...).Result
in some way, but not.
GetJsonAsync(...)).Result
will deadlock
but System.Threading.Tasks.Task.Run(async () => await GetJsonAsync(...)).Result
is not.