等待task.delay有助于更快地刷新UI,但是如何? [英] await task.delay helps in UI refresh faster, but how?

查看:76
本文介绍了等待task.delay有助于更快地刷新UI,但是如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图模型,该视图模型正在获取一行记录并显示在Windows Phone UI上.此获取数据的视图模型方法正在执行许多任务,所有任务都标记有Await操作.

I have a view model that is fetching a row of records and displaying on the Windows phone UI.. This view model method which fetches the data is doing a lot of Tasks, all marked with Await operations..

如下所示:

async Task GetData()
{

    var dataCollection = await GetSomeData();
    await DoThis();
    await DoThat();
}

调用"DoThis"调用后,UI刷新. 现在我只是观察到,如果在其他任务完成之前在代码中引入Task.Delay,则UI会立即刷新.这是我最初的目标,要在"GetSomeData"返回后立即刷新UI,然后触发其他任务.

The UI refreshes after the 'DoThis' call is invoked. Now I just observed that if I introduce a Task.Delay in the code before other Tasks are done, the UI is refreshed immediately.. Which was my original Goal, to refresh the UI immediately after 'GetSomeData' Returns and then fire the other Tasks.

如下所示:

async Task GetData()
{
    var dataCollection = await GetSomeData();
    await Task.Delay(100);
    await DoThis();
    await DoThat();
}

因此我从中了解到,在调用Task.Delay之后,UI线程有机会刷新.但是,如果没有Task.Delay,则如果调用DoThis,则会浪费一些时间,直到它在"DoThis"中找到第一个可等待的方法,以便它可以返回并继续执行UI Refresh.

So what I understand from this, is that the UI thread gets opportunity to refresh after a call to Task.Delay is made. However without Task.Delay, if DoThis is called, some time is lost before it finds the first awaitable method in 'DoThis' so that it can return and continue with UI Refresh.

我的问题是:

  1. 我理解正确吗?
  2. 将其放入生产代码中安全吗?

预先感谢,希望我能解释清楚.

Thanks in advance and hope I have explained clearly..

Pr

下面是这些方法的详细信息,因为没有这些方法,不清楚程序中正在执行什么操作..:(

Below is Details of These methods as without those it is not clear what is going in the program.. :(

async Task<ObservableCollection<MyModel>> GetSomeData()
{
    return await Task.Run(() =>
    {
        using (var db = new MainModelDataContext())
        {
            List<MyModel> temp =
                db.Models.Where(Some condition)
                  .Take(30)
                  .ToList();
            var returnCollection = new ObservableCollection<MyModel>(temp);
            return returnCollection;
        }
}

ObservableCollection绑定到UI页面上的列表控件.页面视图模型正在调用此方法.

The ObservableCollection is bound to a list control on the UI Page. This method is being called by the page view model.

async Task DoThis()
{
   // do some data processing and then post that to the Server 
   // this is the first awaitable method after the data processing

   await (an HttpClientHandler).PostAsync();
}

Task DoThat()也遵循与DoThis相同的流程.数据处理也包装在async-await Tasks中,并且它们仅在某些类属性上起作用.

Task DoThat() also follows the same flow as DoThis.. The data processing is also wrapped in async-await Tasks and they are just working on some class properties.

希望我很清楚..再次谢谢大家

Hope I am clear.. Thanks again all

推荐答案

调用Task.Delay时,您将控制权返回给UI消息循环持续了100毫秒,并且UI消息循环有机会处理来自以下位置的更多消息它的队列.这就是为什么您正在经历刷新"效果的原因.如果您的GetSomeData方法是真正的异步方法,则您的UI应在其操作期间保持响应状态,并在完成后将继续执行下一个await.

When you call Task.Delay, you return control to the UI message loop for those 100 milliseconds, and the UI message loop has the opportunity to process more messages from its queue. That's why you're experiencing the "refreshing" effect. If your GetSomeData method is truely asynchronous, your UI should remain responsive during its operation, and when it completes, it will continue to execute the next await.

如果这种影响没有发生,则意味着您的方法不是真正的异步,并且更有可能它们以 synchronous 运行昂贵的操作阻止您的UI线程的时尚.

If this effect doesn't happen, then that means your methods aren't really asynchronous, and its more likely that they are running a costly operation in a synchronous fashion which is blocking your UI thread.

在将其投入生产之前,您必须研究为什么需要Task.Delay来刷新UI的原因.

Before putting this into production, you have to look into why you need Task.Delay to refresh your UI.

这篇关于等待task.delay有助于更快地刷新UI,但是如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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