Q:

Execute set of tasks in parallel but with a group timeout

I'm currently trying to write a status checking tool with a reliable timeout value.  One way I'd seen how to do this was using Task.WhenAny() and including a Task.Delay, however it doesn't seem to produce the results I expect:
public void DoIUnderstandTasksTest()
{
      var checkTasks = new List<Task>();

      // Create a list of dummy tasks that should just delay or "wait"
      // for some multiple of the timeout
      for (int i = 0; i < 10; i++)
      {
          checkTasks.Add(Task.Delay(_timeoutMilliseconds/2));
      }

      // Wrap the group of tasks in a task that will wait till they all finish
      var allChecks = Task.WhenAll(checkTasks);

      // I think WhenAny is supposed to return the first task that completes
      bool didntTimeOut = Task.WhenAny(allChecks, Task.Delay(_timeoutMilliseconds)) == allChecks;

      Assert.True(didntTimeOut);
}

What am I missing here?

A:

I think you're confusing the workings of the When... calls with Wait.... 
Task.WhenAny doesn't return the first task to complete among those you pass to it. Rather, it returns a new Task that will be completed when any of the internal tasks finish. This means your equality check will always return false - the new task will never equal the previous one.
The behavior you're expecting seems similar to Task.WaitAny, which will block current execution until any of the internal tasks complete, and return the index of the completed task. 
Using WaitAny, your code will look like this:
    // Wrap the group of tasks in a task that will wait till they all finish
  var allChecks = Task.WhenAll(checkTasks);
  var taskIndexThatCompleted = Task.WaitAny(allChecks, Task.Delay(_timeoutMilliseconds));

  Assert.AreEqual(0, taskIndexThatCompleted);

