Question

What is the difference between the TimeSpan private field startTimeStamp and the DateTime.Ticks property? Is there an easy way to retrieve the startTimeStamp (without using reflection) ?

enter image description here

Was it helpful?

Solution

startTimeStamp is a long, it's not a TimeSpan and it's not necessarily DateTime.Ticks.

It will be the time the StopWatch was started, which will be the time based on a high performance counter, or DateTime.Ticks if no high performance counter is available.

You can get the current value for what startTimeStamp is generated from by calling Stopwatch.GetTimeStamp() static method.

The startTimeStamp is not directly exposed but you can calculate it by calling

Stopwatch.GetTimeStamp() - stopwatch.GetRawElapsedTicks() 

OTHER TIPS

Here is the source of the method used to initialize that field:

    public static long GetTimestamp() {
        if(IsHighResolution) { 
            long timestamp = 0;
            SafeNativeMethods.QueryPerformanceCounter(out timestamp);
            return timestamp;
        } 
        else {
            return DateTime.UtcNow.Ticks; 
        } 
    }

    [DllImport(ExternDll.Kernel32)]
    [ResourceExposure(ResourceScope.None)]
    public static extern bool QueryPerformanceCounter(out long value); 

Since this method is public, you can also call it yourself.

You can merely estimate the start time stamp. All you have to do is to call GetTimeStamp() just after starting StopWatch.

System.Diagnostics.Stopwatch watch = new Stopwatch();
watch.Start();
long ts = Stopwatch.GetTimestamp();

enter image description here

and

ts => 62583777603

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top