开发者

How to find the elapsed time between send and receive data?

I am using the System.Timers.Timer class. And I enable when I send a message and disable when I receive the message. How do I calculate how much time has elapsed. I have set the interval as 1 sec

Basically I retransmit data after 1000 sec again if I do not recive an ACK. I retransmit 5 times max until I get a Ack. If I receive and something before 150ms then I stop retrnsmission.

Here's the code:

timer1.interval = 1000;开发者_Go百科
port.Write(data)
timer1.enabled = true;

event handler for the received data.

timer1.enabled=false;


Use Stopwatch instead.

Stopwatch clock = Stopwatch.StartNew();
port.Write(data);

and in the handler

clock.Stop();
Console.WriteLine( clock.Elapsed );


That's not what the System.Timers.Timer class is for. Take a look at System.Diagnostics.Stopwatch instead.


The Timer class is not for measuring elapsed time - it's for generating an event on a regular, timed basis.

What you probably want is the Stopwatch class (visit the link):

var sw = new Stopwatch();
sw.Start();
port.Write(data);
sw.Stop();
var elapased = sw.Elapsed;  // elapsed is a TimeSpan

The Elapsed property is a TimeSpan which has methods and properties for evaluating the amount of time passed in different ways.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜