为什么异步方法使用Task.Run()来挂起UI对象..? [英] Why does an async method hang UI contra using Task.Run()..?

查看:77
本文介绍了为什么异步方法使用Task.Run()来挂起UI对象..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面运行了一个简单的测试示例,它反映了我真实应用中的真实场景。它只包括两个按钮和两个TextBlocks。 

I have run a simple test example below which reflects a realistic scenario in my real app. It just includes two Buttons and two TextBlocks. 

问题:


  • 为什么示例#1使用 await  DoWork()
    挂起整个UI,直到
    工作完成..?我认为使用异步方法的整个想法是一种简单快捷的方式来获取响应式UI?
  • Why does Example#1 using await DoWork() hang the whole UI until the work has been done..? I thought the whole idea with async methods was an easy and quick way to get responsive UI?

  • 示例#2使用 await Task.Run(()=> DoWork2()会很好地更新UI,但是,它会作用于一个单独的线程,并且需要使用Dispather来更新UI和其他对象。比这个..?
  • Example #2 using await Task.Run(()=>DoWork2() does update UI nicely, however, acts on a separate thread and there is a need to use the Dispather to update UI and other objects. Any drawbacks here other than this..?

  • 在编译时, SimulateWork() $ b总会出现警告$ b方法说"..方法缺少等待运算符并且将同步运行,考虑使用等待operarotr等。"确定这个方法是同步的!这就是为什么它首先包装在异步方法/线程中。  有什么方法可以让我们能够获得这些警告吗,我很无聊,我有很多这些警告在我的真实应用程序中。
  • At compile time, there is always a warning on the SimulateWork() method saying "..method lacks await operators and will run synchonously, consider using await operarotr etc.." WELL for sure this method is synchron! That is why it is wrapped in async method/thread in the first place. Is there any way I can get rid of these warnings, I am afriad I have quite a few of them in my real app.

感谢您的任何评论。

        // Example #1
        private async void Button_Click1(object sender, RoutedEventArgs e)
        {
            myLabel1.Text = string.Empty;
            await DoWork1();
        }
        private async Task DoWork1()
        {
            for (int i = 0; i < 100; i++)
            {
                myLabel1.Text = i.ToString();
                await SimulateWork();
            }
        }

        // Example #2
        private async void Button_Click2(object sender, RoutedEventArgs e)
        {
            myLabel2.Text = string.Empty;
            await Task.Run(() => DoWork2());
        }
        private async void DoWork2()
        {
            for (int i = 0; i < 100; i++)
            {
                // Use Dispather here in order to update UI from another thread
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                              {
                                  myLabel2.Text = i.ToString();
                              });
                await SimulateWork();
            }
        }

        // Simulate some synchron work being done in 20 ms
        private async Task SimulateWork()
        {
            SpinWait.SpinUntil(() => false, 20);
        }

推荐答案

编译器告诉你为什么你会看到这个问题。如果您定义了一个调用异步的方法,那么您可以将其从UI线程中删除。等待是你使用的构造,而不是神奇的力量: - )
The compiler is telling you why you see the issue. If you define a method to call async it is up to you to get it off the UI thread. Await is a construct for you to use, not a magical power :-)


这篇关于为什么异步方法使用Task.Run()来挂起UI对象..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆