Search code examples
silverlightwindows-phone-8async-awaitmaps

Windows Phone 8 - Getting Multiple Street Names


I created for Windows Phone 8.0 Silverlight App an async method GetStreetName

string streetname;

private async Task<string> GetStreetName(int i)
{
    MapAddress address;
    ReverseGeocodeQuery query = new ReverseGeocodeQuery();
    query.GeoCoordinate = Route[i].Item1;
    query.QueryCompleted += (s, e) =>
    {
        if (e.Error != null)
            return;

        address = e.Result[0].Information.Address;
        streetname = address.Street;
};
query.QueryAsync();
return streetname;
}

and I call it using the await operator inside of a for loop:

for (int i = 0; i < Route.Count; i++)
{
       ListBox.Items.Add(await GetStreetName(i));
}

but I always get only the street name of the first loaded geoposition back and I have no idea why (I thought the await operator is waiting until the async method is finished).

Additional info: i just saw that this is not 100% clear at this short snippet, streetname and Route are global "variables", Route is a tuple list where the first item is a geocoordinate.

How can I fix this issue?


Solution

  • You are returning from GetStreetName before the results are ready becayse query.QueryAsync(); just starts the query and doesn't wait for it to be complete.

    On top of that, you're writing all results to the same global streetname.

    You need to use a TaskCompletionSource.

    Try something like this:

    private async Task<string> GetStreetNameAsync(int i)
    {
        var tcs = new TaskCompletionSource<IEnumerable<string>>();
    
        EventHandler<QueryCompletedEventArgs<IList<MapLocation>>> handler = (s, e) =>
            {
                if (e.Error != null)
                {
                    tcs.TrySetException(e.Error);
                    return;
                }
    
                if (e.Cancelled)
                {
                    tcs.TrySetCanceled();
                    return;
                }
    
                tcs.TrySetResult(e.Result[0].Information.Address.Street);
            };
    
        var query = new ReverseGeocodeQuery();
        query.GeoCoordinate = Route[i].Item1;
    
        try
        {
            query.QueryCompleted += handler;
    
            query.QueryAsync();
    
            return await tcs.Task;
        }
        finally
        {
            query.QueryCompleted -= handler;
        }
    }