Using Task.wait() application hangs and never returns

PhantomM picture PhantomM · Feb 22, 2013 · Viewed 10.8k times · Source

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");
                    }
                }
            }

Answer

Austin Salonen picture Austin Salonen · Feb 22, 2013

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?