• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

WPF异步模式应用方式

武飞扬头像
51CTO
帮助0

WPF异步模式是一个比较复杂的实现过程。不过我们在这篇文章中将会为大家介绍一下有关WPF异步模式简单的实现方法,方便大家理解。#t#

以WeatherForecast为例. 需求: 用户在窗体上点击一个按钮, 程序去网络上查询天气情况, 并把结果显示在窗体上. 网络查询是一个耗时任务, 在等待结果的同时, 用户将看到一个旋转的时钟动画表示程序正在查询.

WPF异步模式为:

窗口类MainWindow中有耗时函数: string FetchWeatherFromInternet().

窗口类MainWindow中包含一个函数 UpdateUIWhenWeatherFetched(string result), 用于把任务结果显示在界面上.

当用户点击按钮时, 在 btnFetchWeather_Click() 中,

如果是同步调用, 代码很简单, 如下:

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

现在需要异步执行, 稍微麻烦点, 需要用到3个delegate, 其中一个代表要执行的任务, 另一个代表任务结束后的callback, 还有一个代表交给UI执行的任务, 上述WPF异步模式代码被替换成如下:

  1. // 代表要执行的异步任务  
  2. Func<string> asyncAction = 
    this.FetchWeatherFromInternet();  
  3. // 处理异步任务的结果  
  4. Action<IAsyncResult> resultHandler = 
    delegate( IAsyncResult asyncResult )  
  5. {  
  6. //获得异步任务的返回值, 这段代码必须
    在UI线程中执行  
  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. //这是才开始执行异步任务  
  16. asyncAction.BeginInvoke
    ( asyncActionCallback, null );  
  17. private void ForecastButtonHandler
    (object sender, RoutedEventArgs e)  
  18. {  
  19. this.UpdateUIWhenStartFetching
    Weather();  
  20. //异步任务封装在一个delegate中, 
    此delegate将运行在后台线程   
  21. Func<string> asyncAction = this.
    FetchWeatherFromInternet;  
  22. //在UI线程中得到异步任务的返回值,
    并更新UI //必须在UI线程中执行 Action
    <IAsyncResult> resultHandler = 
    delegate(IAsyncResult asyncResult) 
    { string weather = asyncAction.EndInvoke
    (asyncResult); this.UpdateUIWhenWeather
    Fetched(weather); };  
  23. //异步任务执行完毕后的callback, 此callback
    运行在后台线程上. //此callback会异步调用
    resultHandler来处理异步任务的返回值.  
  24. AsyncCallback asyncActionCallback = 
    delegate(IAsyncResult asyncResult)  
  25. {  
  26. this.Dispatcher.BeginInvoke
    (DispatcherPriority.Background, 
    resultHandler, asyncResult);  
  27. };  
  28. //在UI线程中开始异步任务, //asyncAction
    (后台线程), asyncActionCallback(后台线程)
    和resultHandler(UI线程) //将被依次执行  
  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小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /news/detail/tanhbkfbbj
系列文章
更多 icon
同类精品
更多 icon
继续加载