locked
UI synchronization with async/wait

    Question

  • Hi All,

    I am using the async/await pattern in a Windows 8 store app. I am not able to figure how to synchronize the response with the display of UI.

    In one of our pages, we have a list of combo boxes on the left side used as filters for data that will be displayed on the right side. The selected values for the combo boxes are set from a file stored in ApplicationData.Current.LocalFolder. If the file is empty, then the first item in the combo box is set. The function that does "LoadFilters" does three things

    1. Load the file (json object) and convert to UserFilter object. (async call)

    2. Populate combo boxes.

    3. Set values for combo boxes based on the values in the UserFilter object.

    Everything works fine and data on the right side displays expected data but the combo boxes are empty even though the code gets executed (I verified stepping through)

    If I change the code to populate the combo boxes first (swapped steps 1 and 2). Then the combo boxes are populated.

    Here is the async code that gets the file

            private async void LoadFilters(string filterName)
            {
                FileManager fileManager = new FileManager();
                string json = await fileManager.GetPeopleFilterJsonAsync(Constants.PeopleFilterFolderName, filterName);
                PeopleFilter userFilter = JsonConvert.DeserializeObject<PeopleFilter>(json);

    I am encountering similar issues on Login page where though the login is successful, the UI does not execute the code to navigate to the next page. Any thoughts on how I can resolve this issue?

    Thanks,

    Friday, July 11, 2014 10:40 PM

All replies

  • I'm not clear on what exactly you're doing to load the combo boxes. If you databind to an ObservableCollection (or to another collection that supports INotifyCollectionChanged) then you should be able to add your objects to the collection at any time and the combo box will update.

    If you can post to your OneDrive a minimal project demonstrating the problem then we can take a closer look.

    --Rob

    Saturday, July 12, 2014 5:40 AM
    Owner
  • Yes. I am loading it as you have mentioned. The moment, I introduce async/await I see this behavior. Also, I noticed that the thread on which this code runs is a Worker thread....shouldnt it be the Main UI thread?

    Anyway, I will post a solution that demonstrates the problem.




    • Edited by Live_Free Saturday, July 12, 2014 11:40 PM
    Saturday, July 12, 2014 11:40 PM
  • As per my knowledge the Windows 8 model for thread handling is very different from the conventional model that was followed in earlier versions of Windows. In a Windows 8 App (a.k.a Windows Store App), we are supposed to follow the Async-Await pattern wherever possible such that the UI does not get blocked. In fact, this is one of the acceptance guidelines for accepting an App into the Windows Store.

    Looking at your code, my guess is that you are facing a race condition. I suggest introducing a Task.Wait(task object) call to make your code wait for the specified call to complete before moving ahead. Many times, we look for "advanced causes" but tend to overlook the more fundamental ones.

    Friday, April 17, 2015 2:37 PM