成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Silverlight中對多個異步任務的調用

開發 后端
Silverlight中對多個異步任務的調用本身比較麻煩,其思維比較呆板。本文將展示作者是如何去實現這個調用的。

  這是一個常見的問題,由于Silverlight只支持異步調用后臺的服務,而如果有多個任務的話,可能就很麻煩,往往就是要在一個異步任務結束事件中去調用另外一個任務,以此類推。典型的問題就是,代碼很復雜,而且幾乎很難維護。看看下面的代碼吧

  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.   /// <summary>  
  62.   /// 這個枚舉記錄了任務的狀態,默認為Ready  
  63.   /// </summary>  
  64.  publicenumActionStatus  
  65.  {  
  66.   Ready,//準備好,如果最后檢查仍然為這個狀態,則通常表示該任務被跳過了  
  67.   Success,//成功  
  68.   Failure,//失敗  
  69.   Completed//完成  
  70.   }  
  71.   /// <summary>  
  72.   /// 這個記錄了任務的結果  
  73.   /// </summary>  
  74.   publicclassActionResult  
  75. {  
  76.  publicActionResult()  
  77.   {  
  78.   Status = ActionStatus.Ready;//默認為ready  
  79.   StartTime = DateTime.Now;  
  80.  }  
  81. /// <summary>  
  82.   /// 任務名稱  
  83.   /// </summary>  
  84.   publicstringTaskName { getset; }  
  85.   /// <summary>  
  86.   /// 狀態  
  87.   /// </summary>  
  88.  publicActionStatus Status { getset; }  
  89.   /// <summary>  
  90.   /// 消息  
  91.   /// </summary>  
  92.   publicstringMessage { getset; }  
  93.   /// <summary>  
  94.   /// 任務結果  
  95.   /// </summary>  
  96.   publicobjectResult { getset; }  
  97.   /// <summary>  
  98.   /// 開始時間  
  99.   /// </summary>  
  100.   publicDateTime StartTime { getset; }  
  101.   /// <summary>  
  102.   /// 結束時間  
  103.   /// </summary>  
  104.   publicDateTime EndTime { getset; }  
  105.   }  
  106.   /// <summary>  
  107.   /// 異步任務的接口  
  108.   /// </summary>  
  109.  publicinterfaceIAsyncAction  
  110.   {  
  111.   voidExecute();  
  112.   eventEventHandler Completed;  
  113.   ActionResult PreActionResult { getset; }  
  114.   ActionResult ActionResult { getset; }  
  115.   }  
  116.   /// <summary>  
  117.  /// 異步任務的實現類型  
  118.   /// </summary>  
  119.   publicclassAsyncAction : IAsyncAction  
  120.   {  
  121.   publicAsyncAction()  
  122.  {  
  123.   ActionResult = newActionResult();  
  124.   }  
  125.   privateboolAutoComplete = false;  
  126.   privateAction Action { getset; }  
  127.   /// <summary>  
  128.  /// 設置要執行的操作  
  129.   /// </summary>  
  130.   /// <param name="action">操作</param>  
  131.   /// <param name="autoComplete">是否自動完成</param>  
  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.   /// <summary>  
  157.   /// 前置任務的結果,添加這個功能目的是,可能多個任務之間互相有所依賴,例如某個任務要根據前面任務的情況決定是否執行  
  158.   /// </summary>  
  159.   publicActionResult PreActionResult { getset; }  
  160.   /// <summary>  
  161.   /// 當前任務的結果  
  162.  /// </summary>  
  163.   publicActionResult ActionResult { getset; }  
  164.   }  
  165.   /// <summary>  
  166.   /// 任務運行器  
  167.   /// </summary>  
  168.   publicclassAsyncActionRunner  
  169.   {  
  170.   publicAsyncActionRunner()  
  171.   {  
  172.   TaskResults = newList<ActionResult>();  
  173.   }  
  174.   privatereadonlyIEnumerator<IAsyncAction> _enumerator;  
  175.   publicAsyncActionRunner(IEnumerator<IAsyncAction> enumerator)  
  176.   : this()  
  177.   {  
  178.   this._enumerator = enumerator;  
  179.   }  
  180.   publicAsyncActionRunner(IEnumerable<IAsyncAction> tasks)  
  181.   : this()  
  182.   {  
  183.   _enumerator = tasks.GetEnumerator();  
  184.   }  
  185.   /// <summary>  
  186.   /// 完成事件及處理方法  
  187.   /// </summary>  
  188.   publiceventEventHandler Completed;  
  189.   /// <summary>  
  190.   /// 保存所有任務的執行結果  
  191.   /// </summary>  
  192.   publicList<ActionResult> TaskResults { get; privateset; }  
  193.   /// <summary>  
  194.   /// 臨時保存的當前任務的執行結果  
  195.   /// </summary>  
  196.   privateActionResult tmp = null;  
  197.   /// <summary>  
  198.   /// 執行所有任務  
  199.   /// </summary>  
  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.   } 

原文鏈接:http://www.cnblogs.com/chenxizhang/archive/2011/08/30/2159124.html

【編輯推薦】

  1. Windows 8將.NET和Silverlight打入冷宮?
  2. 微軟Silverlight的崛起
  3. Silverlight 5,你的名字是“Windows”
  4. 微軟發布Silverlight 5 beta 附下載
  5. 微軟Silverlight的崛起

 

責任編輯:彭凡 來源: 博客園
相關推薦

2009-12-07 14:35:42

WCF異步調用

2009-08-12 10:47:38

Silverlight

2009-12-31 10:58:31

silverlight

2024-07-31 15:57:41

2024-10-15 10:28:43

2021-03-29 09:26:44

SpringBoot異步調用@Async

2011-11-10 10:00:55

Silverlight

2010-12-01 14:34:59

AsyncTask異步處理任務Android

2009-12-08 14:10:55

Silverlight

2023-01-03 10:38:04

函數計算技術

2022-09-27 12:01:56

Spring異步調用方式

2009-06-03 10:24:11

LoadMaskSilverlight

2009-02-20 08:54:20

DownloaderSilverlight對象

2020-07-02 07:44:27

Spring教程異步

2024-10-14 13:12:59

2023-11-06 14:13:51

asyncio開發

2010-04-23 13:23:42

Silverlight

2010-06-02 09:25:29

Silverlight

2009-11-26 13:12:16

Silverlight

2010-07-12 15:07:05

SQL Server實
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩福利一区 | 国产中文在线 | 啪啪免费 | 国产精品久久在线观看 | 日本一区二区高清不卡 | 亚洲国产精品一区二区三区 | 狠狠入ady亚洲精品经典电影 | 亚洲黄色在线免费观看 | 亚洲一区网站 | 欧美精品欧美精品系列 | 一本色道久久综合亚洲精品高清 | 国产一区二区三区在线免费 | 亚洲精品一区二区在线观看 | 国产一级一级 | 精品日韩一区二区三区av动图 | 午夜影院视频 | 韩日一区 | 久久久久国产一区二区三区四区 | 中文字幕免费视频 | 国产精品完整版 | 色就干| 九九爱这里只有精品 | 久久久久久亚洲 | 国内精品伊人久久久久网站 | 亚洲一区二区视频 | 毛片大全 | 久久精品亚洲成在人线av网址 | 91福利影院| 青青久在线视频 | 亚洲成人中文字幕 | 色香婷婷 | 亚洲国产精品99久久久久久久久 | 97偷拍视频 | 亚洲aⅴ | 亚洲精色 | 亚洲另类春色偷拍在线观看 | 一区二区三区四区日韩 | www.中文字幕.com | www.色综合 | 国产www.| 国产婷婷色一区二区三区 |