Getting System Ticks

  • Thread starter Thread starter Michael Riggio
  • Start date Start date
M

Michael Riggio

Does anyone know a way to get system ticks in .Net? I found DateTime.Ticks,
but that is equal to 100 nanoseconds. The reason I'm asking is because we're
considering passing some sort of timestamp when an event fires. Do you have
any suggestions on getting this value?

Though I haven't confirmed it yet, I would think doing the following would
yield 2 different timestamps:

DateTime x = DateTime.Now;

DateTime y = DateTime.Now;

These should be different, but I'm wondering if they won't be when faster
cpus come out?

Any thoughts?
 
Michael Riggio said:
Does anyone know a way to get system ticks in .Net? I found
DateTime.Ticks, but that is equal to 100 nanoseconds. The reason
I'm asking is because we're considering passing some sort of
timestamp when an event fires. Do you have any suggestions on
getting this value?

I don't think there's a way to get a higher-resolution timer without
resorting to API calls. And, from what I've heard, there's no guarantee
that the hardware will even support one.
Though I haven't confirmed it yet, I would think doing the
following would yield 2 different timestamps:
DateTime x = DateTime.Now;
DateTime y = DateTime.Now;

As I recall, DateTimes only store values at the tick level, so I don't
think that will be any different than using the Ticks property.

If it's absolutely critical that all timestamps be unique, how about the
Ticks value combined with a GUID?

Jeremy
 
Since the event sink will need to determine which events were fired first, I
don't think appending a GUID will help out in this case (though it would
surely make it unique).

Someone else recommended that I have a static long variable that is
essentially an event sequence number. The number gets incremented using
Interlocked.Increment when we fire the event. We simply handle the overflow
when the long goes back to 0.
 
Back
Top