Timer C#. Start, stop, and get the amount of time between the calls

Posted by user1886060 on Stack Overflow See other posts from Stack Overflow or by user1886060
Published on 2012-12-07T16:58:30Z Indexed on 2012/12/07 17:05 UTC
Read the original article Hit count: 138

Filed under:
|
|
|
|

I'm writing 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). Here is my 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 && rcv.ACK01 != ack)
               continue;

           if (Rmsg != null && rcv.ACK01 == ack)
           {
            this.displayMessageDelegate("ack is received :"+ack);
            ChangeAck(ack);
            break;
           }

Thank you.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET