简体   繁体   中英

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..

Looks like below:

async Task GetData()
{

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

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.

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. 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.

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. 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.

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. 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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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