1

How would i go about returning two different kind of results from a task pool that im running in a List of tasks? in my Code i would like to run the two different methods in startCategorysearchAsync at the same time with the help of a task pool but they have different return values one with the category return parameter and one with SubCategoryReturnmparameter SubCategory is a child to Category though? this is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Input;
using DataConverter.Checkers;
using DataConverter.Converters;
using DataConverter.Command;
using DataConverter.Objects;
using AsyncAwaitBestPractices.MVVM;

namespace DataConverter.ViewModels
{
    public class MainWindowViewModel : BaseViewModel
    {

        public List<Category> parentCategories = new List<Category>();
        public List<SubCategory> subCategories1 = new List<SubCategory>(); 


        public string path { get; set; }

       public bool runButtonWorks { get; set; }
       public string errorMessage { get; set; }

       public bool TextIsVisible { get; set; }

       public ICommand run { get; set; }
       private bool _isBusy;
       public bool IsBusy
       {
          get => _isBusy;
          private set => Set(ref _isBusy, value);
       }

        private void Set(ref bool isBusy, bool value)
        {
            throw new NotImplementedException();
        }

        public AsyncCommand start { get; private set;}
       public MainWindowViewModel()
       {
            runButtonWorks = true; 

            start = new AsyncCommand(startCategorysearchAsync);
            start = new AsyncCommand(startSubCategorysearchAsync);




       }
        private async Task startCategorysearchAsync()
        {
            FileCheck check = new FileCheck(path);
            List<List<SubCategory>> subCategoriesList = new List<List<SubCategory>>();
            List<Task> tasks = new List<Task>(); 

            CategoryConverter converter = new CategoryConverter(path);

            try
            {
                runButtonWorks = false;
                tasks.Add(Task.Run(() =>  converter.getCategoryListExcel()));
                tasks.Add(Task.Run(() => converter.getSubCategory()));

                await Task.WhenAll(tasks); 

            }
            finally
            {

            }








        }

        private bool CanExecuteSubmit()
        {
            return !IsBusy;
        }



    }
}

1 Answers1

1

The simplest way would be to let them return their individual result, and combine it with Task.WhenAll(...).

var taskCategoryList = Task.Run(() =>  converter.getCategoryListExcel());
var taskSubCategory = Task.Run(() => converter.getSubCategory());

await Task.WhenAll(taskCategoryList, taskSubCategory);

var resultCategoryList = await taskCategoryList;
var resultSubCategory = await taskSubCategory;

Frank Nielsen
  • 1,546
  • 1
  • 10
  • 17