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

一世执手 提交于 2019-12-11 23:20:37

问题


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 
    }

回答1:


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
}


来源:https://stackoverflow.com/questions/14854180/how-can-i-wait-for-asyncgeocodeservice-geocodeasync-service-method-to-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!