Force Synchronous execution of async method
-
15 Mart 2012 Perşembe 19:24
Hi
I've a method like this
public static async Task<string> GetXmlResponse(string url)
{
string data = null;
using (var client = Connector.GetWebClient())
{
data = await client.DownloadStringTaskAsync(url);
}
return data;}
Now one caller of this method should force that this method executes synchronously.
in the given scenario the download takes less then one second.
Now I tried
var task = GetXmlResponse(url) ;
task.Wait();
But then the code waits for an unlimited time :-(
How can I achieve this?
- Düzenleyen Gentlehag 15 Mart 2012 Perşembe 19:25
Tüm Yanıtlar
-
15 Mart 2012 Perşembe 19:59Moderatör
Hi Gentlehag-
I assume this is in a UI app of some kind? Can you try changing this:
await client.DownloadStringTaskAsync(url);
to be this:
await client.DownloadStringTaskAsync(url).ConfigureAwait(false);
and see if that fixes your deadlock?
- Yanıt Olarak İşaretleyen Stephen Toub - MSFTMicrosoft Employee, Moderator 28 Mart 2012 Çarşamba 16:15