Timer C #. Comece, pare e obtenha o tempo entre as chamadas [duplicado]
Duplicata Possível:
Como medir quanto tempo é uma função em execução?
Estou escrevendo um chat UDP com transferência de dados confiável. Eu preciso iniciar um timer quando um pacote é enviado e pará-lo assim que ele recebe uma resposta do servidor (ACK - reconhecimento).
Aqui está o meu código:
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;
}
Obrigado.