WPF异步模式简单应用方式介绍

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;
  48. }

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

网站栏目:WPF异步模式简单应用方式介绍
浏览地址:http://www.hantingmc.com/qtweb/news47/393547.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联