Force Synchronous execution of async method


  • 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)   ;


    But then the code waits for an unlimited time :-(

    How can I achieve this?

    • נערך על-ידי Gentlehag יום חמישי 15 מרץ 2012 19:25
    יום חמישי 15 מרץ 2012 19:24


  • 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 מרץ 2012 19:59
    מנחה דיון