av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

WPF異步模式簡單應用方式介紹

WPF異步模式是一個比較復雜的實現(xià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)的簡單方法介紹。


文章名稱:WPF異步模式簡單應用方式介紹
文章分享:http://uogjgqi.cn/article/cdhoips.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流