WCF方法“异步调用”的“同步问题”

     在“WCF”中服务方法的调用是“异步”的,而这些异步的方法的执行顺序有先有后,只要当一部分“异步通信”全部完成之后,才能进行下一步的操作,如何使异步通信同步起来?有两种方法,一种是异步用法逐步执行,一种是通过“标志变量”协调执行。不知您是否有其它建议?

     1、顺序执行

          方法1_Async

       方法1_Completed
       方法2_Async

       方法2_Completed
       BindChart()

 

     2、标志变量,协调执行

           int flag=0;

private void BindBaseDatas()
  {
  string weburl = Application.Current.Host.Source.ToString();
  weburl = weburl.Substring(0, (weburl.Length - 23)) + "/ChartsService.svc";
  MyChartsService.ChartsServiceClient client = new MyChartsService.ChartsServiceClient("CustomBinding_ChartsService1", weburl);


  client.GetMonitoringDatasEntityAsync(monitortype, monitordate_type, monitordate_value, monitortype_type, monitortype_value, meterusetype, true);

  client.GetMonitoringDatasEntityCompleted += new EventHandler<GetMonitoringDatasEntityCompletedEventArgs>(client_GetMonitoringDatasEntityCompleted);


client.GetMonitoringDatasEntityTwoAsync(monitortype, monitordate_type, monitordate_value, monitortype_type, monitortype_value, meterusetype, false);
  client.GetMonitoringDatasEntityTwoCompleted += new EventHandler<GetMonitoringDatasEntityTwoCompletedEventArgs>(client_GetMonitoringDatasEntityTwoCompleted);



}

void client_GetMonitoringDatasEntityTwoCompleted(object sender, GetMonitoringDatasEntityTwoCompletedEventArgs e)
  {
  monitoringstwo = e.Result;
     if(flag==0)
     {
           flag=2;
     }
     else
     {
          BindChart(); 
          flag=0;
      }
  }

  void client_GetMonitoringDatasEntityCompleted(object sender, GetMonitoringDatasEntityCompletedEventArgs e)
  {
  monitoringsone = e.Result;
if(flag==0)
     {
           flag=3;
     }
     else
     {
          BindChart(); 
          flag=0;
      }

  }

 

posted on 2012-07-23 10:35  [CC]  阅读(455)  评论(2编辑  收藏  举报

导航