简体   繁体   中英

How can I wait for Async(geocodeService.GeocodeAsync) service/method to complete?

Am using the GeocodeService to get the lattitude and longitude of two places( from, to)

am getting latitude and longitude perfectly but my problem is the message is displaying before async call ( geocodeService.GeocodeAsync ) how can i wait to complete Async call ( geocodeService_GeocodeCompleted ) ??

my code is

private void point_Click(object sender, RoutedEventArgs e)
{
    getCoordinates(fromText.Text, 1);// this is calling after bellow message box :(
    getCoordinates(toText.Text, 2);
    MessageBox.Show(" completed ");  // 1 why this message is displaying first before above calls      
}

private void getCoordinates(string address, int index)
    {
        GeocodeRequest geocodeRequest = new GeocodeRequest();

        // Set the credentials using a valid Bing Maps key
        geocodeRequest.Credentials = new Credentials();
        geocodeRequest.Credentials.ApplicationId = "ApgLkoHIG4rNShRJAxMMNettsv6SWs3eP8OchozFS89Vex7BRHsSbCr31HkvYK-d";

        // Set the full address query
        geocodeRequest.Query = address;

        // Set the options to only return high confidence results 
        FilterBase[] filters = new FilterBase[1];
        filters[0] = new ConfidenceFilter() { MinimumConfidence = Confidence.High };

        GeocodeOptions geocodeOptions = new GeocodeOptions();
        geocodeOptions.Filters = new ObservableCollection<FilterBase>(filters);
        geocodeRequest.Options = geocodeOptions;

        // Make the geocode request
        GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
        geocodeService.GeocodeCompleted += new EventHandler<GeocodeCompletedEventArgs>(geocodeService_GeocodeCompleted);
        geocodeService.GeocodeAsync(geocodeRequest, index);

    }
    void geocodeService_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
    {
            GeocodeResponse geocodeResponse = e.Result;
            fromLat = geocodeResponse.Results[0].Locations[0].Latitude;
            fromLon = geocodeResponse.Results[0].Locations[0].Longitude;
            Messagebox.Show("latitude : "+fromLat); // this is displaying randomly 
    }

Create another method that activates a timer when the async completed is loaded.

pass it the e.result

Have the timer.tick check to see if e.result is length > 0

If it is on tick show window

else show loading or what ever

Something like this

#pretend code#
Global Variable -> string location;

getGeoLoc += new geoLocCompleted;

void geoLocCompleted(sender, e){
   location = e.result
   Timer time = new Timer():
   time.tick += OnTick;

}

void onTick(send, e){
   if(e.result.length > 0)
      Show your results
   else
      Show loading dialog
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM