日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
WPF異步模式簡單應用方式介紹

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

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

WPF異步模式為:

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

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

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

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

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

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

 
 
 
  1. // 代表要執(zhí)行的異步任務
  2. Func asyncAction = 
    this.FetchWeatherFromInternet();
  3. // 處理異步任務的結(jié)果
  4. Action resultHandler = 
    delegate( IAsyncResult asyncResult )
  5. {
  6. //獲得異步任務的返回值, 這段代碼必須
    在UI線程中執(zhí)行
  7. string weather = asyncAction.
    EndInvoke( asyncResult );
  8. this.UpdateUIWhenWeatherFetched
    ( weather );
  9. };
  10. //代表異步任務完成后的callback
  11. AsyncCallback asyncActionCallback = 
    delegate( IAsyncResult asyncResult )
  12. {
  13. this.Dispatcher.BeginInvoke
    ( DispatcherPriority.Background, 
    resultHandler, asyncResult );
  14. };
  15. //這是才開始執(zhí)行異步任務
  16. asyncAction.BeginInvoke
    ( asyncActionCallback, null );
  17. private void ForecastButtonHandler
    (object sender, RoutedEventArgs e)
  18. {
  19. this.UpdateUIWhenStartFetching
    Weather();
  20. //異步任務封裝在一個delegate中, 
    此delegate將運行在后臺線程 
  21. Func asyncAction = this.
    FetchWeatherFromInternet;
  22. //在UI線程中得到異步任務的返回值,
    并更新UI //必須在UI線程中執(zhí)行 Action
     resultHandler = 
    delegate(IAsyncResult asyncResult) 
    { string weather = asyncAction.EndInvoke
    (asyncResult); this.UpdateUIWhenWeather
    Fetched(weather); };
  23. //異步任務執(zhí)行完畢后的callback, 此callback
    運行在后臺線程上. //此callback會異步調(diào)用
    resultHandler來處理異步任務的返回值.
  24. AsyncCallback asyncActionCallback = 
    delegate(IAsyncResult asyncResult)
  25. {
  26. this.Dispatcher.BeginInvoke
    (DispatcherPriority.Background, 
    resultHandler, asyncResult);
  27. };
  28. //在UI線程中開始異步任務, //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;
  48. }

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


網(wǎng)站欄目:WPF異步模式簡單應用方式介紹
本文URL:http://m.5511xx.com/article/cdhoips.html