zoukankan      html  css  js  c++  java
  • 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;
          }

      }
  • 相关阅读:
    Scrapy 扩展中间件: 针对特定响应状态码,使用代理重新请求
    Scrapy 扩展中间件: 同步/异步提交批量 item 到 MySQL
    Scrapy 隐含 bug: 强制关闭爬虫后从 requests.queue 读取的已保存 request 数量可能有误
    Scrapyd 改进第二步: Web Interface 添加 STOP 和 START 超链接, 一键调用 Scrapyd API
    简单示例理解神闭包
    ejs 模板使用方法
    我使用的开源组件汇总,以备学习使用
    了不起的Node.js--之五 TCP连接
    Windows7下Java运行时环境搭建
    了不起的Node.js--之四
  • 原文地址:https://www.cnblogs.com/ssol/p/2604589.html
Copyright © 2011-2022 走看看