This question is not a duplicate. I have already gone through 'Asynchronously wait for Task to complete with timeout' which neither asks nor addresses the issue with Task.Delay(...) inside the task object.
I am writing a piece of code that waits for a condition to become true but I don't want to block the Thread and also doesn't want to eat CPU. And I also want to put a maximum timeout I want to wait before declaring a dead end. Below is how I am trying to do it.
private async Task<bool> WaitForSynchronizedRequest(ClientSynchronizedRequest request, int timeoutInMilliseconds)
{
  bool keepWaiting = true;
  Task task = Task.Factory.StartNew(() =>
  {
    while (keepWaiting)
    {
      if (request.IsCompleted) break;
      Task.Delay(100);
    }
  });
  if (await Task.WhenAny(task, Task.Delay(timeoutInMilliseconds)) != task)
  {
    keepWaiting = false;
  }
  return request.IsCompleted;
}
Task.Delay(...) inside the while loop doesn't wait as expected because of not using async / await but if I use async / await as below then the Task won't wait at all using Task.WhenAny(...) and immediately reports it as completed. Sample code below:
private async Task<bool> WaitForSynchronizedRequest(ClientSynchronizedRequest request, int timeoutInMilliseconds)
{
  bool keepWaiting = true;
  Task task = Task.Factory.StartNew(async () =>
  {
    while (keepWaiting)
    {
      if (request.IsCompleted) break;
      await Task.Delay(100);
    }
  });
  if (await Task.WhenAny(task, Task.Delay(timeoutInMilliseconds)) != task)
  {
    keepWaiting = false;
  }
  return request.IsCompleted;
}
Any suggestion for achieving the Delay with a Timeout as well (without blocking the Thread)?
 
    