Peter Duniho schrieb:
On 2007-11-08 05:39:01 -0800, Daniel Kay <da*********@arcor.desaid:
>Hello again,
after further searching on the net I found the class
System.Diagnostics.StopWatch. I can start that stopwatch when my
library/application starts, and use the Elapsed Property. Any doubts
that this is a good idea? Or ar there better ways?
A Stopwatch instance would be fine. You could also just use
DateTime.Now to obtain the current time, subtracting from that a
previously acquired time to obtain a TimeSpan value. I would say those
are the two most common time-keeping techniques.
You should keep in mind that even though Stopwatch and DateTime
ostensibly provide millisecond precision, they aren't actually accurate
to the nearest millisecond. The nature of Windows thread scheduling
will prevent you from getting timing information more accurate than to
tens of milliseconds.
Finally, note that there are actually timer classes (at least three that
I'm aware of). Depending on what the original code did with the time
information, it may be better to use a timer. In particular, you
probably would not want to poll the current time on a very frequent
basis (e.g. in a tight loop).
Thanks for your reply. The library I like to port receives data from the
serial port in a message based protocol. I save every received byte in
an object, which retrieves the current timestamp. When a message is
completly received I check if any abnormal delays occured during
transmission by calculating the timespan between the first and last
received byte. Knowing the transfer speed I can decide whether to accept
and process the message or not. If for instance a message took 44ms to
transmit, but I calculated an expected time of 12ms, I ignore the
message. I am not so strict on the thresholds, because the UART may
delay the interrupt if the FIFO isn't totally filled up. But at the end
I don't want to use the systemtime, because the user may change the time
while I receive a message and then messages would be dropped which
weren't received delayed.
The serialport is usally set at 19200bps and data isn't received all the
time. So I do not expect that polling the stopwatch time does use to
much system resources. Thanks again for your comments. It seems that
it's not the worst solution I have found. :-)
CU,
Daniel
CU,
Daniel