繁体   English   中英

如何等待Async(geocodeService.GeocodeAsync)服务/方法完成?

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

现在用的是GeocodeService得到lattitudelongitude的两个地方(从,到)

我正在完美地获得纬度和经度,但是我的问题是在异步调用( geocodeService.GeocodeAsync )之前显示消息,我如何等待完成异步调用( geocodeService_GeocodeCompleted )?

我的代码是

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 
    }

创建另一个方法,该方法将在异步完成加载后激活计时器。

通过电子结果

让timer.tick检查e.result是否为length> 0

如果在刻度显示窗口中

否则显示加载或任何

像这样

#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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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