Force Synchronous execution of async method
-
15. března 2012 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?
- Upravený Gentlehag 15. března 2012 19:25
Všechny reakce
-
15. března 2012 19:59Moderátor
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?
- Označen jako odpověď Stephen Toub - MSFTMicrosoft Employee, Moderator 28. března 2012 16:15