title: await Task.Delay takes longer than expected
body: <p>I wrote a multithreaded app which uses async/await extensively. It is supposed to download some stuff at a scheduled time. To achieve that, it uses 'await Task.Delay'. Sometimes it sends thousands requests every minute.</p>
  
  <p>It works as expected, but sometimes my program needs to log something big. When it does, it serializes many objects and saves them to a file. During that time, I noticed that my scheduled tasks are executed too late. I've put all the logging to a separate thread with the lowest priority and the problem doesn't occur that often anymore, but it still happens. The things is, I want to know when it happens and in order to know that I have to use something like that:</p>
  
  <pre><code>var delayTestDate = DateTime.Now;
  await Task.Delay(5000);
  if((DateTime.Now - delayTestDate).TotalMilliseconds &gt; 6000/*delays up to 1 second are tolerated*/) Console.WriteLine("The task has been delayed!");
  </code></pre>
  
  <p>Moreover, I have found that 'Task.Run', which I also use, can also cause delays. To monitor that, I have to use even more ugly code:</p>
  
  <pre><code>var delayTestDate = DateTime.Now;
  await Task.Run(() =&gt;
  {
    if((DateTime.Now - delayTestDate).TotalMilliseconds &gt; 1000/*delays up to 1 second are tolerated*/) Console.WriteLine("The task has been delayed!");
    //do some stuff
    delayTestDate = DateTime.Now;
  });
  if((DateTime.Now - delayTestDate).TotalMilliseconds &gt; 1000/*delays up to 1 second are tolerated*/) Console.WriteLine("The task has been delayed!");
  </code></pre>
  
  <p>I have to use it before and after every await and Task.Run and inside every async function, which is ugly and inconvenient. I can't put it into a separate function, since it would have to be async and I would have to await it anyway. Does anybody have an idea of a more elegant solution?</p>
  
  <p>EDIT:</p>
  
  <p>Some information I provided in the comments:</p>
  
  <p>As @YuvalItzchakov noticed, the problem may be caused by Thread Pool starvation. That's why I used <code>System.Threading.Thread</code> to take care of the logging outside of the Thread Pool, but as I said, the problem still sometimes occur.</p>
  
  <p>I have a processor with four cores and by subtracting results of <code>ThreadPool.GetAvailableThreads</code> from <code>ThreadPool.GetMaxThreads</code> I get 0 busy worker threads and 1-2 busy completion port threads. <code>Process.GetCurrentProcess().Threads.Count</code> usually returns about 30. It's a Windows Forms app and although it only has a tray icon with a menu, it starts with 11 threads. When it gets to sending thousands requests per minute, it quickly gets up to 30.</p>
  
  <p>As @Noseratio suggested, I tried to play with <code>ThreadPool.SetMinThreads</code> and <code>ThreadPool.SetMaxThreads</code>, but it didn't even change the numbers of busy threads mentioned above.</p>