title: C# Async ApiController route issue resolving Task.WhenAny with a Task.Delay
body: <p>On my server I have a route that generates a pdf for a user.  When the generation takes longer than a specified amount of time, the route is supposed to return an accepted status code and send a email to the user when the processing is finished.  The issue I am having is that the <code>Task.Delay</code> is not being respected.  Not matter how low I set <code>PdfGenerationTimeLimitUntilEmail</code> the <code>Wait.Any</code> still does not resolve until the <code>createPdfTask</code> is finished.  Interestingly enough when the generation throws an exception or completes, and the time has exceeded the <code>Task.Delay</code> time, execution proceeds to return the Accepted Status.  My suspicion is that the culprit is a DeadLock situation. </p>
  
  <p>A related question which I have read thoroughly, but have not been able to apply to my problem:
  <a href="https://stackoverflow.com/questions/23984655/c-net-4-5-why-does-await-task-whenany-never-return-when-provided-with-a-ta">C#/.NET 4.5 - Why does &quot;await Task.WhenAny&quot; never return when provided with a Task.Delay in a WPF application&#39;s UI thread?</a> </p>
  
  <p>Is there anything obvious that I am missing? Or something about the thread context that I should know?</p>
  
  <pre><code> [HttpPost]
   [Route("foo")]
   public async Task&lt;IHttpActionResult&gt; Foo([FromBody] FooBody fooBody)
   {
          var routeUser = await ValidateUser();
  
          async Task&lt;Stream&gt; CeatePdfFile()
          {
              var pdf = await createPdfFromFooData(fooBody);
  
              return await pdfFileToStream(pdf);
          }
  
          var delay = Task.Delay(PdfGenerationTimeLimitUntilEmail);
          var createPdfTask = CeatePdfFile();
          var firstTaskResolved = await Task.WhenAny(createPdfTask , delay);
  
          if (firstTaskResolved == createPdfTask)
          {
              var pdfFileStream = await createPdfTask ;
              return new FileActionResult(pdfFileStream);
          }
  
          // Creating the PDF can take a long time, so just send an email when it's done
          async void SendEmail(CancellationToken token)
          {
              var pdfFileStreamToEmail = await createPdfTask ;
              _emailSender.SendDownloadEmail(routeUser.Email, pdfFileStreanToEmail);
          }
  
          HostingEnvironment.QueueBackgroundWorkItem(token =&gt; SendEmail(token));
          return StatusCode(HttpStatusCode.Accepted);
  }
  </code></pre>