locked
Force Synchronous execution of async method

    Dotaz

  • 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?



    15. března 2012 19:24

Odpovědi

  • 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?

    15. března 2012 19:59