I'm trying to find out which approach is better let's say we have a button, after user clicks it we perform 1. Send a async request using httpClient
2. Some heavy synchronous staff like computations and saving data to a database.
Like that:
button1.Click += async(sender, e) =>
{
bool a = await Task.Run(async () => { return await MyTask1();});
}
async Task<bool> MyTask1()
{
await new HttpClient().GetAsync("https://www.webpage.com");
DoHeavyStuffFor5minutes();
return true;
}
button2.Click += async(sender, e) =>
{
bool a = await MyTask2();
}
async Task<bool> MyTask2()
{
await new HttpClient().GetAsync("https://www.webpage.com").ConfigureAwait(false);
DoHeavyStuffFor5minutes();
}
From what i understand GetAsync
does not block my UI thread because underneath it uses a method which make it runs on different thread perhaps Task.Run
or any other method that allows that.
But DoHeavyStuffFor5Minutes
will block my UI because it will get called on the caller SynchornizationContext
.
So i read that using ConfigureAwait(false)
will make code after GetAsync
do not run on the same SynchornizationContext as the caller. My question is, which approach is better first or the second one?