title: awaiting task with timeout
body: <p>I'm trying to write a helper method which allows me to pass in an arbitrary task and a timeout. If the task completes before the timeout, a success delegate is called, otherwise an error delegate is called. The method looks like this:</p>
  
  <pre><code>    public static async Task AwaitWithTimeout(Task task, int timeout, Action success, Action error)
      {
          if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
          {
              if (success != null)
              {
                  success();
              }
          }
          else
          {
              if (error != null)
              {
                  error();
              }
          }
      }
  </code></pre>
  
  <p>Now this <em>seems</em> to work most of the time, but i wanted to write some tests as well to make sure. This test, to my surprise fails, and calls the error delegate instead of the success:</p>
  
  <pre><code>        var taskToAwait = Task.Delay(1);
          var successCalled = false;
  
          await TaskHelper.AwaitWithTimeout(taskToAwait, 10, () =&gt; successCalled = true, null);
  
          Assert.IsTrue(successCalled);
  </code></pre>
  
  <p>This test, however, is green:</p>
  
  <pre><code>        var taskToAwait = Task.Run(async () =&gt;
          {
              await Task.Delay(1);
          });
  
          var successCalled = false;
  
          await TaskHelper.AwaitWithTimeout(taskToAwait, 10, () =&gt; successCalled = true, null);
  
          Assert.IsTrue(successCalled);
  </code></pre>
  
  <p>How do i make both tests green? Is my usage of Task.WhenAny incorrect?</p>