title: Timer C#. Start, stop, and get the amount of time between the calls
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/10107140/how-to-measure-how-long-is-a-function-running">How to measure how long is a function running?</a>  </p>
  </blockquote>
  
  
  
  <p>I'm writing a UDP chat with reliable data transfer. I need to start a timer when a packet is sent, and stop it as soon it receives an answer from the server (ACK - acknowledgment). </p>
  
  <p>Here is my code: </p>
  
  <pre><code> private void sendButton_Click(object sender, EventArgs e)
   {
       Packet snd = new Packet(ack, textBox1.Text.Trim());
       textBox1.Text = string.Empty;
       Smsg = snd.GetDataStream();//convert message into array of bytes to send.
       while (true)
       {
          try
           {  // Here I need to Start a timer!
             clientSock.SendTo(Smsg, servEP); 
             clientSock.ReceiveFrom(Rmsg, ref servEP);
             //Here I need to stop a timer and get elapsed amount of time.
  
             Packet rcv = new Packet(Rmsg);
             if (Rmsg != null &amp;&amp; rcv.ACK01 != ack)
                 continue;
  
             if (Rmsg != null &amp;&amp; rcv.ACK01 == ack)
             {
              this.displayMessageDelegate("ack is received :"+ack);
              ChangeAck(ack);
              break;
             }
  </code></pre>
  
  <p>Thank you.</p>