简体   繁体   English

等待task.delay有助于更快地刷新UI,但是如何?

[英]await task.delay helps in UI refresh faster, but how?

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.. 我有一个视图模型,该视图模型正在提取一行记录并在Windows Phone UI上显示。.这种提取数据的视图模型方法正在执行许多任务,所有任务都标记有Await操作。

Looks like below: 如下图所示:

async Task GetData()
{

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

The UI refreshes after the 'DoThis' call is invoked. 调用“ DoThis”调用后,UI刷新。 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. 现在,我刚刚观察到,如果在其他任务完成之前在代码中引入Task.Delay,则UI会立即刷新。这是我最初的目标,要在“ GetSomeData”返回后立即刷新UI,然后触发其他任务。

Looks like below: 如下图所示:

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

So what I understand from this, is that the UI thread gets opportunity to refresh after a call to Task.Delay is made. 因此,据我了解,在调用Task.Delay之后,UI线程就有机会刷新。 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. 但是,如果没有Task.Delay,则如果调用DoThis,则会浪费一些时间,直到它在“ DoThis”中找到第一个可等待的方法,以便它可以返回并继续执行UI Refresh。

My questions are: 我的问题是:

  1. Have I understood it correct? 我了解正确吗?
  2. Is it safe to put this in production code? 将其放入生产代码中安全吗?

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

The ObservableCollection is bound to a list control on the UI Page. ObservableCollection绑定到UI页面上的列表控件。 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() 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. Task DoThat()也遵循与DoThis相同的流程。数据处理也包装在async-await Tasks中,它们仅在某些类属性上起作用。

Hope I am clear.. Thanks again all 希望我清楚..再次感谢大家

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. 当您调用Task.Delay ,您将控制权返回到UI消息循环持续了100毫秒,并且UI消息循环有机会处理其队列中的更多消息。 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 . 如果您的GetSomeData方法是真正的异步方法,则您的UI应在其操作期间保持响应状态,并在完成后将继续执行下一个await

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. 如果这种影响没有发生,则意味着您的方法并不是真正的异步方法,并且更有可能它们以同步方式运行昂贵的操作,从而阻塞了您的UI线程。

Before putting this into production, you have to look into why you need Task.Delay to refresh your UI. 在将其投入生产之前,您必须研究为什么需要Task.Delay来刷新UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM