wpf – 在不冻结UI的情况下运行长任务
发布时间:2020-12-14 04:10:50 所属栏目:Windows 来源:网络整理
导读:我正在尝试在后台执行操作,而不会冻结UI. 当然,我可以使用BackgroundWorker. 但是,我只想使用Task API. 我试过了: async void OnTestLoaded(object sender,RoutedEventArgs e){ await LongOperation();}// It freezes the UI 和 async void OnTestLoaded(ob
我正在尝试在后台执行操作,而不会冻结UI.
当然,我可以使用BackgroundWorker. 但是,我只想使用Task API. 我试过了: async void OnTestLoaded(object sender,RoutedEventArgs e) { await LongOperation(); } // It freezes the UI 和 async void OnTestLoaded(object sender,RoutedEventArgs e) { var task = Task.Run(()=> LongOperation()); task.Wait(); } // It freezes the UI 我应该回到BackgroundWorker吗?或者只有使用任务的解决方案?
你非常接近.
async void OnTestLoaded(object sender,RoutedEventArgs e) { await Task.Run(() => LongOperation()); } 异步does not execute a method on a thread pool thread. Task.Run在线程池线程上执行操作,并返回表示该操作的Task. 如果在异步方法中使用Task.Wait,则为doing it wrong.您应该等待异步方法中的任务,永远不会阻塞它们. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在WIN2008R2的IIS7环境下安装PHP5.6.15
- windows – 如何计算Webserver带宽使用情况
- windows-7 – 用于具有触摸界面的禁用控件的UX设计
- windows – 如何增加apache连接限制? (WAMP)
- Windows Phone – 以编程方式获取手机口音刷C#
- windows-7 – VirtualBox:粮食表不一致
- .net – DateTime.Parse在XP vs Windows 7/8上有所不同
- .net项目dll内嵌加载
- windows-phone-8.1 – 如何避免StorageFile.CopyAsync()在复
- assembly – 自定义键盘中断处理程序
推荐文章
站长推荐
热点阅读