我正在使用System.Timers.Timer类。我在发送消息时启用,并在收到消息时禁用。如何计算已经过了多长时间。我将间隔设置为1秒
基本上,如果我没有收到ACK,我会在1000秒后重新传输数据。我最多转发5次,直到得到Ack。如果我在150ms之前收到了什么,那么我就停止重新开始了。
以下是代码:
timer1.interval = 1000;
port.Write(data)
timer1.enabled = true;
接收数据的事件处理程序。
timer1.enabled=false;
答案 0 :(得分:6)
改用秒表。
Stopwatch clock = Stopwatch.StartNew();
port.Write(data);
并在处理程序
中clock.Stop();
Console.WriteLine( clock.Elapsed );
答案 1 :(得分:1)
这不是System.Timers.Timer类的用途。请改为查看System.Diagnostics.Stopwatch。
答案 2 :(得分:0)