WPF异步模式简单应用方式介绍
创始人
2024-06-18 11:50:15
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 asyncAction = 
    this.FetchWeatherFromInternet();  
  3. // 处理异步任务的结果  
  4. Action 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 asyncAction = this.
    FetchWeatherFromInternet;  
  22. //在UI线程中得到异步任务的返回值,
    并更新UI //必须在UI线程中执行 Action
     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;  

以上就是对WPF异步模式实现的简单方法介绍。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...