locked
Having trouble with async call in windows store app.

    Question

  • Hi,

    I am having an issue with some async coding that i can not explain

                        UploadProgressRing.Visibility = Visibility.Visible;
                        UploadProgressRing.IsActive = true;

                        foreach (var item in ItemsGridView.SelectedItems)
                        {

                            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                            {
                                try
                                {
                                    //Process item
                                    await Processor.ProcessItem(item);
                                    //delete item
                                    DeleteItem(item);
                                }
                                catch (Exception x)
                                {
                                    MessageDialog msgDialog = new MessageDialog(x.Message, "Error Occurred");
                                    msgDialog.ShowAsync();
                                    return;
                                }
                            });
                        }


                        UploadProgressRing.Visibility = Visibility.Collapsed;
                        UploadProgressRing.IsActive = false;

    The issue that i am having is that it appears that the

    await Processor.ProcessItem(item);

    line of code is not waiting.

    Debugging is showing me that the code which hides the Progress ring is executing before the for loop has finished.

    Does anyone have a recommendation for how to overcome this?

    Thanks cjm

    Monday, October 06, 2014 10:26 PM

Answers

  • This is happening because the Dispatcher.RunAsync line is just scheduling that work to be done on the UI Thread.  It doesn't mean it is going to run it then return.  Move the code that collapses and disables the UploadProgressRing  to after the DeleteItem call. 

    Bret Bentzinger (MSFT) @awehellyeah

    • Marked as answer by c.j.mcnaught Tuesday, October 07, 2014 1:39 AM
    Monday, October 06, 2014 11:26 PM
    Moderator

All replies

  • I'm not familiar with Processor.ProcessItem(item).

    What's the return value of this method? If that method returns void and it's not returning a task then the caller will not wait for the task to complete. 

    Have you tried moving the code for the progress ring into the Dispatched code block?
    • Edited by Bryan Stump Monday, October 06, 2014 11:28 PM
    Monday, October 06, 2014 11:26 PM
  • This is happening because the Dispatcher.RunAsync line is just scheduling that work to be done on the UI Thread.  It doesn't mean it is going to run it then return.  Move the code that collapses and disables the UploadProgressRing  to after the DeleteItem call. 

    Bret Bentzinger (MSFT) @awehellyeah

    • Marked as answer by c.j.mcnaught Tuesday, October 07, 2014 1:39 AM
    Monday, October 06, 2014 11:26 PM
    Moderator
  • its a class/method i have defined, i have had to make it an async process as it calls something that i am forced to await.

    It's signature is: public static async Task<bool> ProcessItem(var)

    Monday, October 06, 2014 11:43 PM
  • Thanks for the advice Bret.

    It is an improvement on what i have got at this stage.

    It would still be nice to be able to execute some code when the entire for loop has completed the processing, at this stage i am only able to run some code after each item is complete.

    Monday, October 06, 2014 11:55 PM
  • Maybe you can create a function that deletes the items, and schedule that function to run on the UI Thread.   In that function you can show the progress bar, delete items(don't need to schedule on UI thread because function is already running on it), then re-enable them.  That would probably make the most sense.


    Bret Bentzinger (MSFT) @awehellyeah

    Tuesday, October 07, 2014 4:31 PM
    Moderator