Using Task.wait() application hangs and never returns
Asked Answered
H

2

13

I am new to C# and using Task. I was trying to run this application but my application hangs every time. When I am adding task.wait(), it keeps waiting and never returns. Any help is much appreciated. EDIT: I want to call DownloadString Asynchronously. And when I am doing task.Start() as suggested by "Austin Salonen" I don't get the address of location but default value in location string from returnVal. It means that location got value assigned before task got completed. How can I make sure that after task is completed only then location gets assigned returnVal.

public class ReverseGeoCoding
        {
                static string baseUri = "http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false";
                string location = "default";
                static string returnVal = "defaultRet";
                string latitude = "51.962146";
                string longitude = "7.602304";
                public string getLocation()
                {
                    Task task = new Task(() => RetrieveFormatedAddress(latitude, longitude));  
                //var result = Task.Factory.StartNew(RetrieveFormatedAddress("51.962146", "7.602304"));
                    task.Wait();
                    //RetrieveFormatedAddress("51.962146", "7.602304");
                    location = returnVal;
                    return location;
                }
                public static void RetrieveFormatedAddress(string lat, string lng)
                {
                    string requestUri = string.Format(baseUri, lat, lng);

                    using (WebClient wc = new WebClient())
                    {
                        wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
                        wc.DownloadStringAsync(new Uri(requestUri));
                    }
                }

                static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
                {
                    var xmlElm = XElement.Parse(e.Result);

                    var status = (from elm in xmlElm.Descendants()
                                  where elm.Name == "status"
                                  select elm).FirstOrDefault();
                    if (status.Value.ToLower() == "ok")
                    {
                        var res = (from elm in xmlElm.Descendants()
                                   where elm.Name == "formatted_address"
                                   select elm).FirstOrDefault();
                        //Console.WriteLine(res.Value);
                        returnVal = res.Value;
                    }
                    else
                    {
                        returnVal = "No Address Found";
                        //Console.WriteLine("No Address Found");
                    }
                }
            }
Honeysweet answered 22/2, 2013 at 18:20 Comment(0)
B
3

I don't understand why you use DownloadStringCompleted event and try to make it blocking. If you want to wait the result, just use blocking call DownloadString

var location = RetrieveFormatedAddress(51.962146, 7.602304);

string RetrieveFormatedAddress(double lat, double lon)
{
    using (WebClient client = new WebClient())
    {
        string xml = client.DownloadString(String.Format(baseUri, lat, lon));
        return ParseXml(xml);
    }
}

private static string ParseXml(string xml)
{
    var result = XDocument.Parse(xml)
                .Descendants("formatted_address")
                .FirstOrDefault();
    if (result != null)
        return result.Value;
    else
        return "No Address Found";
}

If you want to make it async

var location = await RetrieveFormatedAddressAsync(51.962146, 7.602304);

async Task<string> RetrieveFormatedAddressAsync(double lat,double lon)
{
    using(HttpClient client = new HttpClient())
    {
        string xml =  await client.GetStringAsync(String.Format(baseUri,lat,lon));
        return ParseXml(xml);
    }
}
Bowden answered 22/2, 2013 at 19:4 Comment(0)
T
12

You aren't actually running the task. Debugging and checking task.TaskStatus would show this.

// this should help
task.Start();

// ... or this:
Task.Wait(Task.Factory.StartNew(RetrieveFormatedAddress("51.962146", "7.602304")));

Though if you're blocking, why start another thread to begin with?

Toothbrush answered 22/2, 2013 at 18:22 Comment(2)
I want to call DownloadString Asynchronously. And when I am doing task.Start(); I don't get the address of location but default value in location string from returnVal. It means that location got value assigned before task got completed. How can I make sure that after task is completed only then location gets assigned returnVal.Honeysweet
@Austin, can you please explain what's behind this behavior? i.e. the Task's code does run when simply called without StartNew, but never returns from the first await.Symbol
B
3

I don't understand why you use DownloadStringCompleted event and try to make it blocking. If you want to wait the result, just use blocking call DownloadString

var location = RetrieveFormatedAddress(51.962146, 7.602304);

string RetrieveFormatedAddress(double lat, double lon)
{
    using (WebClient client = new WebClient())
    {
        string xml = client.DownloadString(String.Format(baseUri, lat, lon));
        return ParseXml(xml);
    }
}

private static string ParseXml(string xml)
{
    var result = XDocument.Parse(xml)
                .Descendants("formatted_address")
                .FirstOrDefault();
    if (result != null)
        return result.Value;
    else
        return "No Address Found";
}

If you want to make it async

var location = await RetrieveFormatedAddressAsync(51.962146, 7.602304);

async Task<string> RetrieveFormatedAddressAsync(double lat,double lon)
{
    using(HttpClient client = new HttpClient())
    {
        string xml =  await client.GetStringAsync(String.Format(baseUri,lat,lon));
        return ParseXml(xml);
    }
}
Bowden answered 22/2, 2013 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.