Silverlight中对多个异步任务的调用

这是一个常见的问题,由于Silverlight只支持异步调用后台的服务,而如果有多个任务的话,可能就很麻烦,往往就是要在一个异步任务结束事件中去调用另外一个任务,以此类推。典型的问题就是,代码很复杂,而且几乎很难维护。看看下面的代码吧

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于网站设计、成都网站设计、斗门网络推广、成都小程序开发、斗门网络营销、斗门企业策划、斗门品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供斗门建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

 
 
 
 
  1.   //传统的多个异步任务的调用方法,必须是一层一层嵌套的方式
  2.   var proxy = newServiceReference1.WebService1SoapClient();
  3.   proxy.Endpoint.Address = newSystem.ServiceModel.EndpointAddress(
  4.   newUri(App.Current.Host.Source, "../WebService1.asmx"));
  5.   proxy.HelloWorldCompleted += (o, a) =>
  6.   {
  7.   proxy.GetEmployeeCompleted += (o1, a1) =>
  8.   {
  9.   proxy.GetCustomersCompleted += (o2, a1) =>
  10.   {
  11.   };
  12.   proxy.GetCustomersAsync();
  13.   };
  14.   proxy.GetEmployeeAsync();
  15.   };
  16.   proxy.HelloWorldAsync();

  为了解决这个问题,我自己也想过一些办法,同时参考了张志敏的如下文章

  http://www.cnblogs.com/beginor/archive/2010/12/24/1915910.html

  这篇文章提供了一个不错的思路。这篇文章的评论中,有朋友也提到了Reactive Framework,我看了看,还没有找到很好的应用方法。这个Framework是一个很强大的东西,但在本文讨论的场景中具体该如何应用,如果有这方面研究的朋友,请不吝赐教

  在这篇文章提供的简单模型基础上,我做了一些修改,并且也增加了一些更加实用的特性。共享出来给大家参考

  添加和改进的功能主要是:

  1.使用更加便捷(原先是用IEnumerator去构造Runner,现在提供了更多的支持,可以是一个Array,也可以是一个List等等,因为我们很多时候任务是动态构造出来的)

  2.提供了任务结果反馈(ActionResult)的功能

  3.提供了任务之间约束的功能,在每个任务里面都可以得到前置任务的信息

  如何使用?

  第一步:添加Nuget Package,关于什么是Nuget,请参考 http://www.cnblogs.com/dudu/archive/2011/07/15/nuget.html

第二步,参考如下的范例代码

  运行效果

可以直接复制这个代码进行使用或者修改

 
 
 
 
  1.   usingSystem;
  2.   usingSystem.Collections.Generic;
  3.   /*
  4.   * 这个设计针对在Silverlight中经常需要对多个远程服务进行调用,而且我们可能需要让这些任务之间有固定的顺序,同时还希望能够在任务之间传递任务状态。
  5.   * 作者:陈希章
  6.   * 时间:2011年8月30日
  7.   * 反馈:ares@xizhang.com
  8.   */
  9.   #regionSample Code
  10.   ////第一个任务
  11.   //var task = new AsyncAction();
  12.   //task.SetAction(() =>
  13.   //{
  14.   // var proxy = new ServiceReference1.WebService1SoapClient();
  15.   // proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(
  16.   // new Uri(App.Current.Host.Source, "../WebService1.asmx"));
  17.   // proxy.HelloWorldCompleted += (o, a) =>
  18.   // {
  19.   // task.ActionResult.TaskName = "Hello,world";
  20.   // task.ActionResult.Message = "Test test";
  21.   // task.ActionResult.Result = a.Result;
  22.   // task.ActionResult.Status = ActionStatus.Success;
  23.   // task.OnCompleted();
  24.   // };
  25.   // proxy.HelloWorldAsync();
  26.   //}, true);
  27.   ////第二个任务
  28.   //var task2 = new AsyncAction();
  29.   //task2.SetAction(() =>
  30.   //{
  31.   // var proxy = new ServiceReference1.WebService1SoapClient();
  32.   // proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(
  33.   // new Uri(App.Current.Host.Source, "../WebService1.asmx"));
  34.   // proxy.HelloWorldCompleted += (o, a) =>
  35.   // {
  36.   // task2.ActionResult.TaskName = "Hello,world";
  37.   // task2.ActionResult.Message = "Test test";
  38.   // task2.ActionResult.Result = a.Result;
  39.   // task2.ActionResult.Status = ActionStatus.Success;
  40.   // task2.OnCompleted();
  41.   // };
  42.   // proxy.HelloWorldAsync();
  43.   //}, true);
  44.   ////构造Runner
  45.   //var runner = new AsyncActionRunner(new[] { task, task2 });
  46.   ////注册完成事件
  47.   //runner.Completed += (o, a) =>
  48.   //{
  49.   // //将界面设置为空闲
  50.   // busyIndicator.IsBusy = false;
  51.   // //显示所有任务的执行结果
  52.   // dgResult.ItemsSource = runner.TaskResults;
  53.   //};
  54.   ////将界面设置为忙碌
  55.   //busyIndicator.IsBusy = true;
  56.   ////执行
  57.   //runner.Execute();
  58.   #endregion
  59.   namespaceSystem
  60.   {
  61.   /// 
  62.   /// 这个枚举记录了任务的状态,默认为Ready
  63.   /// 
  64.  publicenumActionStatus
  65.  {
  66.   Ready,//准备好,如果最后检查仍然为这个状态,则通常表示该任务被跳过了
  67.   Success,//成功
  68.   Failure,//失败
  69.   Completed//完成
  70.   }
  71.   /// 
  72.   /// 这个记录了任务的结果
  73.   /// 
  74.   publicclassActionResult
  75. {
  76.  publicActionResult()
  77.   {
  78.   Status = ActionStatus.Ready;//默认为ready
  79.   StartTime = DateTime.Now;
  80.  }
  81. /// 
  82.   /// 任务名称
  83.   /// 
  84.   publicstringTaskName { get; set; }
  85.   /// 
  86.   /// 状态
  87.   /// 
  88.  publicActionStatus Status { get; set; }
  89.   /// 
  90.   /// 消息
  91.   /// 
  92.   publicstringMessage { get; set; }
  93.   /// 
  94.   /// 任务结果
  95.   /// 
  96.   publicobjectResult { get; set; }
  97.   /// 
  98.   /// 开始时间
  99.   /// 
  100.   publicDateTime StartTime { get; set; }
  101.   /// 
  102.   /// 结束时间
  103.   /// 
  104.   publicDateTime EndTime { get; set; }
  105.   }
  106.   /// 
  107.   /// 异步任务的接口
  108.   /// 
  109.  publicinterfaceIAsyncAction
  110.   {
  111.   voidExecute();
  112.   eventEventHandler Completed;
  113.   ActionResult PreActionResult { get; set; }
  114.   ActionResult ActionResult { get; set; }
  115.   }
  116.   /// 
  117.  /// 异步任务的实现类型
  118.   /// 
  119.   publicclassAsyncAction : IAsyncAction
  120.   {
  121.   publicAsyncAction()
  122.  {
  123.   ActionResult = newActionResult();
  124.   }
  125.   privateboolAutoComplete = false;
  126.   privateAction Action { get; set; }
  127.   /// 
  128.  /// 设置要执行的操作
  129.   /// 
  130.   /// 操作
  131.   /// 是否自动完成
  132.   publicvoidSetAction(Action action, boolautoComplete)
  133.   {
  134.  Action = action;
  135.   AutoComplete = autoComplete;
  136.   }
  137.   publicvirtualvoidExecute()
  138.   {
  139.   if(Action != null)
  140.   {
  141.   ActionResult.StartTime = DateTime.Now;
  142.   Action();
  143.   if(!AutoComplete)
  144.   OnCompleted();
  145.   }
  146.   }
  147.   publiceventEventHandler Completed;
  148.   publicvoidOnCompleted()
  149.   {
  150.   var completed = this.Completed;
  151.   if(completed != null)
  152.  {
  153.  completed(this, EventArgs.Empty);
  154.   }
  155.   }
  156.   /// 
  157.   /// 前置任务的结果,添加这个功能目的是,可能多个任务之间互相有所依赖,例如某个任务要根据前面任务的情况决定是否执行
  158.   /// 
  159.   publicActionResult PreActionResult { get; set; }
  160.   /// 
  161.   /// 当前任务的结果
  162.  /// 
  163.   publicActionResult ActionResult { get; set; }
  164.   }
  165.   /// 
  166.   /// 任务运行器
  167.   /// 
  168.   publicclassAsyncActionRunner
  169.   {
  170.   publicAsyncActionRunner()
  171.   {
  172.   TaskResults = newList();
  173.   }
  174.   privatereadonlyIEnumerator _enumerator;
  175.   publicAsyncActionRunner(IEnumerator enumerator)
  176.   : this()
  177.   {
  178.   this._enumerator = enumerator;
  179.   }
  180.   publicAsyncActionRunner(IEnumerable tasks)
  181.   : this()
  182.   {
  183.   _enumerator = tasks.GetEnumerator();
  184.   }
  185.   /// 
  186.   /// 完成事件及处理方法
  187.   /// 
  188.   publiceventEventHandler Completed;
  189.   /// 
  190.   /// 保存所有任务的执行结果
  191.   /// 
  192.   publicList TaskResults { get; privateset; }
  193.   /// 
  194.   /// 临时保存的当前任务的执行结果
  195.   /// 
  196.   privateActionResult tmp = null;
  197.   /// 
  198.   /// 执行所有任务
  199.   /// 
  200.   publicvoidExecute()
  201.   {
  202.   if(this._enumerator.MoveNext())
  203.   {
  204.   this._enumerator.Current.Completed += (sender, args) =>
  205.   {
  206.   tmp = ((IAsyncAction)sender).ActionResult;
  207.   tmp.EndTime = DateTime.Now;
  208.   TaskResults.Add(tmp);
  209.   this.Execute();
  210.   };
  211.   this._enumerator.Current.PreActionResult = tmp;
  212.   this._enumerator.Current.Execute();
  213.   }
  214.   else
  215.   {
  216.   var completed = this.Completed;
  217.  if(completed != null)
  218.   {
  219.   completed(this, EventArgs.Empty);
  220.   }
  221.   }
  222.   }
  223.   }
  224.   }

文章标题:Silverlight中对多个异步任务的调用
分享路径:http://www.shufengxianlan.com/qtweb/news45/277395.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联