locked
Task.WhenAll behaviour issue RRS feed

  • Question

  • Hi ,

    I am new to windows 8 developing and also async programming.

    I have 2 async methods.

     public async Task<bool> GetCategories(Catalog catalog)
            {
                var ecats = catalog.Categories;
                ItemsToLoadCounter = ecats.Count();

                IEnumerable<Task<CategoryViewModel>> categoriesQuery = from cat in ecats
                                                        select ProcessCategory(cat, catalog);
                Task<CategoryViewModel>[] categoryTasks = categoriesQuery.ToArray();
                CategoryViewModel[] categories = await Task.WhenAll(categoryTasks);
                int indexCounter = 1;
                foreach (var viewModel in categories) {
                    if (viewModel.HasSubCategories || (viewModel.Cards.Count > 0))
                    {
                        viewModel.Index = indexCounter++;
                        viewModel.SelectedSubCategoryChanged += viewModel_SelectedSubCategoryChanged;

                        viewModel.ScallopedPaperImage = string.Format("/Assets/ScallopedPaper/paper_{0}.png",
                                                                      (indexCounter%9) + 1);
                        Categories.Add(viewModel);
                    }
                    ItemsLoaded++;
                }

                if (IsInDesignMode)
                {
                    SelectedCategory = Categories[2];
                }
                return true;
            }


     private async Task<CategoryViewModel> ProcessCategory(Category cat, Catalog catalog) {
                var aViewModel = new CategoryViewModel(cat, null, this, catalog.Nebs, _dataService, _navigationService,
                                                       _applicationSettings, _settingsVM,_googleAnalytics);
                var junk=await aViewModel.GetCategoryDetails();
                return aViewModel;
            }

    Categories is the collection that i need to get filled but while debugging i can see that after  this line :

       CategoryViewModel[] categories = await Task.WhenAll(categoryTasks);

    it returns from my GetCategories method and it never gets to foreach loop where it adds the viewModel to the Categories.

    In categoryTasks there are 14 tasks and i can see the status for all of them  before going to Task.WhenAll(status:wait for validation and result:not computed yet).

    Those other tasks sould get me the viewmodel for adding to Categories.

    But i do not understand why it breaks after Task.WhenAll

    Shouldnt this start another task after all the categoryTasks are completed?

    Wednesday, November 6, 2013 7:42 AM

All replies

  • Can you link a minimal runnable repro project in your SkyDrive and clarify what the expected behavior is and how it differs from the actual behavior?

    The function will always return from "await" so that it can get back to pumping messages and not hang your thread. It will continue again after when the awaited Task completes.

    --Rob

    Thursday, November 7, 2013 2:48 AM
    Moderator
  • Unfortunately i cannot , but what if one of the tasks(there are 14 tasks waited to be executed ) is not executed and throws an execption?

    Should it still return after the "await" line and continue the code?

    Is there anyway that i can catch an eventual exception ?

    Thanks!

    Thursday, November 7, 2013 7:41 AM
  • I'm not sure what you mean. It would help greatly if you could provide and annotate a minimal runnable sample demonstrating the behavior you are getting and explaining what you would prefer.

    --Rob

    Friday, November 8, 2013 4:20 AM
    Moderator