Yi-Wei Ang
Yi-Wei Ang

Reputation: 203

Waiting for async operation to complete

I have a question about how asynchronous methods work on c#. I am trying to get the convertAddressToCoordinate method to set the myLocation variable to a GeoCoordinate. However, the compareDistance method is called even before the myLocation value is set. How can I ensure that the myLocation value is not null before I call compareDistance()?

public GeoCoordinate myLocation = null;

public void returnClosestCurrent(string address)
 {

     convertAddressToCoordinate(address);
     compareDistance(myLocation);

 }

 public void convertAddressToCoordinate(string add)
 {
    WebClient wc = new WebClient();
     wc.DownloadStringCompleted += wc_DownloadStringCompleted;
     wc.DownloadStringAsync(new Uri("http://maps.googleapis.com/maps/api/geocode/json?address=1600+bay+st&sensor=false"));


 }

 void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
 {

    XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);

     var data = from query in xdoc.Descendants("location")
                 select new Location
                 {
                     lat = (string)query.Element("lat"),
                     lng = (string)query.Element("lng")
                 };

     GeoCoordinate destinationGeo = new GeoCoordinate(Convert.ToDouble(data.ElementAt(0).lat), Convert.ToDouble(data.ElementAt(0).lng));

     myLocation = destinationGeo;


 }

Upvotes: 1

Views: 1963

Answers (3)

Dustin Kingen
Dustin Kingen

Reputation: 21245

You would want to await the wc.DownloadStringAsync

public async Task returnClosestCurrent(string address)
{
    await convertAddressToCoordinate(address)
              .ContinueWith(t => compareDistance(myLocation));
}

public async Task convertAddressToCoordinate(string add)
{
    WebClient wc = new WebClient();
    wc.DownloadStringCompleted += wc_DownloadStringCompleted;
    await wc.DownloadStringAsync(new Uri("http://maps.googleapis.com/maps/api/geocode/json?address=1600+bay+st&sensor=false"));
}

The pattern with async/await is the code tends to turn everything into async/await

It could be refactored to use WebClient.DownloadStringTaskAsync

public async Task ReturnClosestCurrent(string address)
{
    await convertAddressToCoordinate(address)
            .ContinueWith(t => compareDistance(t.Result));
}

public async Task<GeoCoordinate> ConvertAddressToCoordinate(string add)
{
    WebClient wc = new WebClient();
    wc.DownloadStringCompleted += wc_DownloadStringCompleted;
    var content = await wc.DownloadStringTaskAsync(new Uri("http://maps.googleapis.com/maps/api/geocode/json?address=1600+bay+st&sensor=false"));

    return ParseContent(content);
}

private GeoCoordinate ParseContent(string content)
{
    XDocument xdoc = XDocument.Parse(content, LoadOptions.None);

    var data = from query in xdoc.Descendants("location")
             select new Location
             {
                 lat = (string)query.Element("lat"),
                 lng = (string)query.Element("lng")
             };

    GeoCoordinate destinationGeo = new GeoCoordinate(Convert.ToDouble(data.ElementAt(0).lat), Convert.ToDouble(data.ElementAt(0).lng));

    return destinationGeo;
}

Upvotes: 1

Pedro Lamas
Pedro Lamas

Reputation: 7233

Looking at your code, seems to me that the compareDistance(myLocation); should be called only in the end of the wc_DownloadStringCompleted method.

After that change, everything should work fine.

Upvotes: 1

Joan Caron
Joan Caron

Reputation: 1970

you can use the await keyword for getting values of async task

http://msdn.microsoft.com/en-us/library/vstudio/hh156528.aspx

Upvotes: 2

Related Questions