title: Task.Delay(<ms>).Wait(); sometimes causing a 15ms delay in messaging system
body: <p>I have 3 programs talking to each other. One of my programs is giving me a strange issue, which is causing my latency measurements to be off.</p>
  
  <p>All 3 applications use the same Custom communications library...</p>
  
  <p>In the problematic application, when the program starts, a new task is started to continuously watch a queue for the arrival of a message:</p>
  
  <pre><code>public void Init()
  {
      var ip = "127.0.0.1";
      CommunicationsManager.UdpManager.Initialize(ip, (int)UDPPort.FromUnityToPredictor, (int)UDPPort.FromPredictorToUnity);
      CommunicationsManager.UdpManager.UdpMessageReceived += OnMessageReceived;
      CommunicationsManager.UdpManager.StartListening();
  
      var task = Task.Run(() =&gt;
      {
          ProcessMessageQueueThread();
      });
  
      //  Thread thread = new Thread(ProcessMessageQueueThread);
      //  thread.IsBackground = true;
      //  thread.Start();
  }
  
  private void ProcessMessageQueueThread()
  {
      while (true)
      {
              if (MessageQueue.Count &gt; 0)
              {
                  ProcessMessage();
              }
      }
  }
  </code></pre>
  
  <p>I a have a function subscribed to an event listener, which fires upon the arrival of a new UDP datagram:</p>
  
  <pre><code>private void OnMessageReceived(object sender, UDPMessage message)
  {
      MessageQueue.Enqueue(message);
      //Task.Run(() =&gt;
      //{
      //ProcessMessage();
      //});
  }
  </code></pre>
  
  <p>Upon the function firing, the message is added to a <code>BlockingCollection</code> queue.</p>
  
  <p>The message is then processed by <code>ProcessMessage</code>:</p>
  
  <pre><code>static Stopwatch sw = new Stopwatch();
  
  private void ProcessMessage()
  {
      var message = MessageQueue.Dequeue();
      messagesReceived++;
      sw.Restart();
  
      //if server side prediction, delay before reading...
      if (!message.ClientSidePrediction)
      {
          // Thread.Sleep(message.UpDelay);
          Task.Delay(message.UpDelay).Wait();
      }
  
      //If prediction must not be used...
      if (!message.UsePrediction)
      {
          message.IsAPredictedMessage = false;
          CommunicationsManager.UdpManager.Send(message);
          messagesSent++;
          return;
      }
  
      if (message.UsePrediction&amp;&amp; messagesReceived == 1)
      {
  
          message.IsAPredictedMessage = false;
          CommunicationsManager.UdpManager.Send(message);
          logger.Info("First message sent:" + sw.ElapsedMilliseconds);
          sw.Restart();
          messagesSent++;
      }
  
      message.IsAPredictedMessage = true;
      CommunicationsManager.UdpManager.Send(message);
      logger.Info("second message sent:" + sw.ElapsedMilliseconds);
      messagesSent++;
  
      // Console.WriteLine("Sent:" + messagesSent + ", Received: " + messagesReceived);
  
  }
  </code></pre>
  
  <p>As can be seen, I am simulating a network delay in `ProcessMessage':</p>
  
  <pre><code>Task.Delay(message.UpDelay).Wait();
  </code></pre>
  
  <p>If I set <code>message.UpDelay</code> to 30, I will see a delay between sending messages of 45ms or 46ms... if I set the <code>message.UpDelay</code> to 1, I will see a sending delay of 15ms or 16ms...</p>
  
  <p>However, most of the time the delay is correct... ie, 30ms/31 ms, or 1ms/2ms.
  Is seems that at some random (at least, to me it appears random) time, the delay takes an additional 15ms/16ms to complete...</p>
  
  <p>What is going on? It is very important that my delays are consistent and accurate. I have tried using <code>Thread.Sleep</code> and creating threads rather than using tasks (can be seen in the code comments), but I do not see a difference.</p>