偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

WPF異步模式是一個比較復雜的實現(xiàn)過程。不過我們在這篇文章中將會為大家介紹一下有關(guān)WPF異步模式簡單的實現(xiàn)方法,方便大家理解。#t#

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

 

WPF異步模式為:

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

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

當用戶點擊按鈕時, 在 btnFetchWeather_Click() 中,

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

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

現(xiàn)在需要異步執(zhí)行, 稍微麻煩點, 需要用到3個delegate, 其中一個代表要執(zhí)行的任務(wù), 另一個代表任務(wù)結(jié)束后的callback, 還有一個代表交給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ù)封裝在一個delegate中, 
    此delegate將運行在后臺線程   
  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
    運行在后臺線程上. //此callback會異步調(diào)用
    resultHandler來處理異步任務(wù)的返回值.  
  24. AsyncCallback asyncActionCallback = 
    delegate(IAsyncResult asyncResult)  
  25. {  
  26. this.Dispatcher.BeginInvoke
    (DispatcherPriority.Background, 
    resultHandler, asyncResult);  
  27. };  
  28. //在UI線程中開始異步任務(wù), //asyncAction
    (后臺線程), asyncActionCallback(后臺線程)
    和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;  

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

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

2009-12-25 09:34:50

WPF顯示HTML

2009-12-28 13:23:19

WPF導出圖片

2009-12-23 18:06:25

WPF模板

2009-12-29 10:32:24

WPF Listbox

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-07-29 15:15:31

ASP應(yīng)用程序

2009-12-29 08:54:09

Ubuntu Linu

2009-12-23 10:29:01

WPF應(yīng)用程序

2010-01-25 13:29:53

Android本地應(yīng)用

2010-04-15 14:33:16

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

2009-12-11 13:48:44

PBR策略路由

2009-12-21 17:45:26

Fedora Core

2010-07-07 10:37:09

SAN多協(xié)議

2010-02-22 14:09:08

WCF Dispose

2009-12-28 10:47:58

WPF繪圖

2010-02-04 14:29:45

C++ typenam

2010-02-06 16:21:35

C++常規(guī)DLL
點贊
收藏

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