No they are not both based on clock tick.
Stopwatch
maybe either high or low res. If low res, then it usesDateTime.UtcNow
underneith. Unfortuantely you cannot choose if it's high or low, so:Create own "Stopwatch" that always uses
DateTime.UtcNow
underneith.
EDIT
That's a stupid suggestion in (2.), you obviously need to avoid DateTime.UtcNow
as that's what you are trying to correct. I suggest you look at working in ticks, by which I mean 1/10000 of a second, to match high-res Stopwatch
. This is because TimeSpan
is only accurate to 1/1000 of a second.
Number 1. in more detail:
Stopwatch
uses this method:
public static long GetTimestamp()
{
if (!Stopwatch.IsHighResolution)
{
DateTime utcNow = DateTime.UtcNow;
return utcNow.Ticks; //There are 10,000 of these ticks in a second
}
else
{
long num = (long)0;
SafeNativeMethods.QueryPerformanceCounter(out num);
return num; //These ticks depend on the processor, and
//later will be converted to 1/10000 of a second
}
}
But like I say, IsHighResolution
appears to be not settable and as a static
applies system wide anyway, so write your own.