自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WPF異步模式簡(jiǎn)單應(yīng)用方式介紹

開發(fā) 開發(fā)工具
WPF異步模式的創(chuàng)建對(duì)于初學(xué)者來說還是比較難理解的。在這里,我們可以通過一個(gè)簡(jiǎn)單的實(shí)現(xiàn)案例來為大家介紹相關(guān)實(shí)現(xiàn)方法。

WPF異步模式是一個(gè)比較復(fù)雜的實(shí)現(xiàn)過程。不過我們?cè)谶@篇文章中將會(huì)為大家介紹一下有關(guān)WPF異步模式簡(jiǎn)單的實(shí)現(xiàn)方法,方便大家理解。#t#

以WeatherForecast為例. 需求: 用戶在窗體上點(diǎn)擊一個(gè)按鈕, 程序去網(wǎng)絡(luò)上查詢天氣情況, 并把結(jié)果顯示在窗體上. 網(wǎng)絡(luò)查詢是一個(gè)耗時(shí)任務(wù), 在等待結(jié)果的同時(shí), 用戶將看到一個(gè)旋轉(zhuǎn)的時(shí)鐘動(dòng)畫表示程序正在查詢.

 

WPF異步模式為:

窗口類MainWindow中有耗時(shí)函數(shù): string FetchWeatherFromInternet().

窗口類MainWindow中包含一個(gè)函數(shù) UpdateUIWhenWeatherFetched(string result), 用于把任務(wù)結(jié)果顯示在界面上.

當(dāng)用戶點(diǎn)擊按鈕時(shí), 在 btnFetchWeather_Click() 中,

如果是同步調(diào)用, 代碼很簡(jiǎn)單, 如下:

  1. string result = this.
    FetchWeatherFromInternet();  
  2. this.UpdateUserInterface( result ); 

現(xiàn)在需要異步執(zhí)行, 稍微麻煩點(diǎn), 需要用到3個(gè)delegate, 其中一個(gè)代表要執(zhí)行的任務(wù), 另一個(gè)代表任務(wù)結(jié)束后的callback, 還有一個(gè)代表交給UI執(zhí)行的任務(wù), 上述WPF異步模式代碼被替換成如下:

 

  1. // 代表要執(zhí)行的異步任務(wù)  
  2. Func<string> asyncAction = 
    this.FetchWeatherFromInternet();  
  3. // 處理異步任務(wù)的結(jié)果  
  4. Action<IAsyncResult> resultHandler = 
    delegate( IAsyncResult asyncResult )  
  5. {  
  6. //獲得異步任務(wù)的返回值, 這段代碼必須
    在UI線程中執(zhí)行  
  7. string weather = asyncAction.
    EndInvoke( asyncResult );  
  8. this.UpdateUIWhenWeatherFetched
    ( weather );  
  9. };  
  10. //代表異步任務(wù)完成后的callback  
  11. AsyncCallback asyncActionCallback = 
    delegate( IAsyncResult asyncResult )  
  12. {  
  13. this.Dispatcher.BeginInvoke
    ( DispatcherPriority.Background, 
    resultHandler, asyncResult );  
  14. };  
  15. //這是才開始執(zhí)行異步任務(wù)  
  16. asyncAction.BeginInvoke
    ( asyncActionCallback, null );  
  17. private void ForecastButtonHandler
    (object sender, RoutedEventArgs e)  
  18. {  
  19. this.UpdateUIWhenStartFetching
    Weather();  
  20. //異步任務(wù)封裝在一個(gè)delegate中, 
    此delegate將運(yùn)行在后臺(tái)線程   
  21. Func<string> asyncAction = this.
    FetchWeatherFromInternet;  
  22. //在UI線程中得到異步任務(wù)的返回值,
    并更新UI //必須在UI線程中執(zhí)行 Action
    <IAsyncResult> resultHandler = 
    delegate(IAsyncResult asyncResult) 
    { string 
    weather = asyncAction.EndInvoke
    (asyncResult); this.UpdateUIWhenWeather
    Fetched(weather); };  
  23. //異步任務(wù)執(zhí)行完畢后的callback, 此callback
    運(yùn)行在后臺(tái)線程上. //此callback會(huì)異步調(diào)用
    resultHandler來處理異步任務(wù)的返回值.  
  24. AsyncCallback asyncActionCallback = 
    delegate(IAsyncResult asyncResult)  
  25. {  
  26. this.Dispatcher.BeginInvoke
    (DispatcherPriority.Background, 
    resultHandler, asyncResult);  
  27. };  
  28. //在UI線程中開始異步任務(wù), //asyncAction
    (后臺(tái)線程), asyncActionCallback(后臺(tái)線程)
    和resultHandler(UI線程) //將被依次執(zhí)行  
  29. asyncAction.BeginInvoke(asyncAction
    Callback, null);  
  30. }  
  31. private string FetchWeatherFromInternet()  
  32. {  
  33. // Simulate the delay from network access.  
  34. Thread.Sleep(4000);  
  35. String weather = "rainy";  
  36. return weather;  
  37. }  
  38. private void UpdateUIWhenStartFetching
    Weather()  
  39. {  
  40. // Change the status this.fetchButton.
    IsEnabled
     = false;  
  41. this.weatherText.Text = "";  
  42. }  
  43. private void UpdateUIWhenWeatherFetched
    (string weather)  
  44. {  
  45. //Update UI text  
  46. this.fetchButton.IsEnabled = true;  
  47. this.weatherText.Text = weather;  

以上就是對(duì)WPF異步模式實(shí)現(xiàn)的簡(jiǎn)單方法介紹。

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2009-12-25 09:34:50

WPF顯示HTML

2009-12-29 10:32:24

WPF Listbox

2009-12-28 13:23:19

WPF導(dǎo)出圖片

2009-12-23 18:06:25

WPF模板

2009-12-24 14:30:19

WPF MVVM

2009-06-23 13:01:04

JSF應(yīng)用

2010-03-10 15:05:22

linux系統(tǒng)備份方式

2009-12-25 13:41:33

2009-12-29 08:54:09

Ubuntu Linu

2009-07-29 15:15:31

ASP應(yīng)用程序

2009-12-23 10:29:01

WPF應(yīng)用程序

2010-01-25 13:29:53

Android本地應(yīng)用

2009-12-11 13:48:44

PBR策略路由

2010-04-15 14:33:16

WiMAX無線應(yīng)用模式

2009-12-28 10:47:58

WPF繪圖

2009-12-21 17:45:26

Fedora Core

2010-07-07 10:37:09

SAN多協(xié)議

2010-02-06 16:21:35

C++常規(guī)DLL

2010-02-04 14:29:45

C++ typenam

2010-02-22 14:09:08

WCF Dispose
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)