可能重复:
如何衡量一个函数运行了多长时间?
我正在编写一个具有可靠数据传输的 UDP 聊天。我需要在发送数据包时启动一个计时器,并在它收到来自服务器的应答(ACK - 确认)后立即停止它。
这是我的代码:
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;
}
谢谢你。