title: Delay in while loop and its precision
body: <p>I am writing C# code to make data acquire system from FPGA with USB communication and not that familiar with C#.
  The system received data continuously at regular intervals through USB.
  Data is continuously received only when <code>DOPPLER_NUM = 1</code>, and <code>while (DOPPLER_NUM == 1)</code> is that process.
  And there are two problems in operation.</p>
  <ol>
  <li><p>When I operate code without <code>Delay(1)</code> in while loop, the program is completely stopped and none of the buttons in the program works.</p>
  </li>
  <li><p>I used <code>Delay</code> function to solve the problem, and it works.
  But, the delay I want is 1ms and it actually varies from 1-15ms when I measured it with Stopwatch function.</p>
  </li>
  </ol>
  <pre><code>public void Delay(int MS)
  {
      DateTime ThisMoment = DateTime.Now;
      TimeSpan Duration = new TimeSpan(0, 0, 0, 0, MS);
      DateTime AfterWards = DateTime.Now.Add(Duration);
  
      while (AfterWards &gt;= ThisMoment)
      {
           System.Windows.Forms.Application.DoEvents();
           ThisMoment = DateTime.Now;
      }
      return;
  }
  </code></pre>
  <pre><code>int DOPPLER_NUM = 0;
  
  private void Doppler_Start_Click(object sender, EventArgs e)
  {       
       int transmit_mode = 1; 
  
       if (DOPPLER_NUM == 0)
       {
            DOPPLER_NUM = 1;
            transmit_mode = 1;
       }
       else
       {
            DOPPLER_NUM = 0;
            transmit_mode = 0;
            cnt4GS = 0;
       }
  
       CyBulkEndPoint Inednpt1;
       Inednpt1 = MyDevice.EndPointOf(0x86) as CyBulkEndPoint;
  
       int bytes1 = Convert.ToInt32(256);
       int bytes2 = bytes1 * 2;
  
       bool bXferCompleted = false;
  
       bool IsPkt = false;
  
       byte[] buffer4GS = new byte[400 * 256];
  
       byte[] buffer1 = new byte[bytes2];
       byte[] Data_buf = new byte[bytes1];
  
       while (DOPPLER_NUM == 1)
       {
            Stopwatch clk1 = new Stopwatch();
            clk1.Start();
  
            Delay(1);
    
            clk1.Stop();
            opertime.Text = (clk1.ElapsedTicks * 100 / 1000).ToString() + &quot; us&quot;;                    
  
            if (MyDevice != null)
            {
                 if (Inednpt1 != null)
                 {                           
                     bXferCompleted = Inednpt1.XferData(ref buffer1, ref bytes2, IsPkt); // Data receiving from USB
                                                             
                     Data_buf = Doppler_Processing(buffer1, bytes2);
               
                     if (cnt4GS &gt;= 0 &amp;&amp; cnt4GS &lt;= 399)
                     {
                           Buffer.BlockCopy(Data_buf, 0, buffer4GS, cnt4GS * 256, 256);
                           cnt4GS++;
                      }
                      else if (cnt4GS &gt;= 400)
                      {
                           Buffer.BlockCopy(buffer4GS, 256, buffer4GS, 0, 102144);
                           Buffer.BlockCopy(Data_buf, 0, buffer4GS, 102144, 256);
                       }
  
                       Grayscale(buffer4GS);                               
                  }                           
             }
             else if (MyDevice == null)
             {
                 MessageBox.Show(&quot;ERROR. NODEVICE.&quot;, &quot;Error Message&quot;);
                 break;
             }
         }
  </code></pre>