1

Using TPL, i have multiple tasks set up (created dynamically, could be a large number), each returns a bool, i want to check if all task return values are true. how would i do this? is it possible? if not, is it possible to pass in a shared object to each task and have the tasks update this object?

EDIT: This would be an example of my task. ProcessEntity returns a bool. Now I have multiple of these tasks created and executed. I need to check that all the results are true.

 private Task<bool> CreateTask(MyEntity entity, Action onStart, Action onComplete)
    {
        return (new Task<bool>(
            () =>
            {
                onStart.Invoke();
                var result = false;
                try
                {
                    result = ProcessEntity(myEntity);
                }
                catch (Exception ex)
                {
                }

                onComplete.Invoke();
                return result;
            })
               );
    }

 for (int i = 0; i < counter; i++)
        {
            CreateTask(entities[i], () => _taskCounter++, () => _taskCounter--).Start();
        }

So at this point I need to continue execution of other code and that must only happen if all tasks returned true.

newbie_86
  • 4,520
  • 17
  • 58
  • 89
  • All of these are possible. If you were to show us your code and what you've tried that didn't work, it would be easier to help. – Matt Smith Apr 15 '13 at 13:34
  • possible duplicate of [Limit number of tasks with TPL](http://stackoverflow.com/questions/16012096/limit-number-of-tasks-with-tpl) This appears to be about the same problem. Rather than create new questions revise the current. You have had several requests for specifics. – paparazzo Apr 15 '13 at 14:30
  • Do you want to synchronously wait for the `Task`s to complete or do this asynchronously? Also, what should happen if some of the `Task`s fail? – svick Apr 15 '13 at 14:40
  • @Blam I don't think so. This seems to be a separate question, though both contain very little specifics. – svick Apr 15 '13 at 15:03
  • @Blam...its a different question, it has nothing to do with controlling the number of threads, it's about accessing the results – newbie_86 Apr 15 '13 at 16:18

1 Answers1

2

Simply query the Task.Result property of each, this will both wait for the task to complete and return the result:

void Main()
{
    var tasks = new List<Task<bool>>();

    // spawn all the tasks
    for (int index = 0; index < 10; index++)  
        tasks.Add(Task.Factory.StartNew(new Func<bool>(GetValue)));

    // now wait for them to return
    bool didAllReturnTrue = tasks.All(t => t.Result);
    // note that if one task returns false, the rest of the tasks will not be
    // waited upon, and will finish in their own time.

    // show the results (LINQPad syntax)
    didAllReturnTrue.Dump();
}

public bool GetValue()
{
    Thread.Sleep(500);
    return true;
}

Note that this will not handle exceptions in any of the tasks well, you will have to build that in. Also, with the new async/await support in C# 5.0 / .NET 4.5, you can write the above code to be a little bit more async-friendly as well, but I'll leave that alone for this answer.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • thanks! i need all the tasks to complete first and then to access the results and continue with further processing – newbie_86 Apr 15 '13 at 16:17