mscorlib(4.0.0.0) API with additions
Stopwatch.cs
1 using Microsoft.Win32;
2 
3 namespace System.Diagnostics
4 {
6  [global::__DynamicallyInvokable]
7  public class Stopwatch
8  {
9  private const long TicksPerMillisecond = 10000L;
10 
11  private const long TicksPerSecond = 10000000L;
12 
13  private long elapsed;
14 
15  private long startTimeStamp;
16 
17  private bool isRunning;
18 
20  [global::__DynamicallyInvokable]
21  public static readonly long Frequency;
22 
24  [global::__DynamicallyInvokable]
25  public static readonly bool IsHighResolution;
26 
27  private static readonly double tickFrequency;
28 
32  [global::__DynamicallyInvokable]
33  public bool IsRunning
34  {
35  [global::__DynamicallyInvokable]
36  get
37  {
38  return isRunning;
39  }
40  }
41 
44  [global::__DynamicallyInvokable]
45  public TimeSpan Elapsed
46  {
47  [global::__DynamicallyInvokable]
48  get
49  {
50  return new TimeSpan(GetElapsedDateTimeTicks());
51  }
52  }
53 
56  [global::__DynamicallyInvokable]
57  public long ElapsedMilliseconds
58  {
59  [global::__DynamicallyInvokable]
60  get
61  {
62  return GetElapsedDateTimeTicks() / 10000;
63  }
64  }
65 
68  [global::__DynamicallyInvokable]
69  public long ElapsedTicks
70  {
71  [global::__DynamicallyInvokable]
72  get
73  {
74  return GetRawElapsedTicks();
75  }
76  }
77 
78  static Stopwatch()
79  {
80  if (!Microsoft.Win32.SafeNativeMethods.QueryPerformanceFrequency(out Frequency))
81  {
82  IsHighResolution = false;
83  Frequency = 10000000L;
84  tickFrequency = 1.0;
85  }
86  else
87  {
88  IsHighResolution = true;
89  tickFrequency = 10000000.0;
90  tickFrequency /= Frequency;
91  }
92  }
93 
95  [global::__DynamicallyInvokable]
96  public Stopwatch()
97  {
98  Reset();
99  }
100 
102  [global::__DynamicallyInvokable]
103  public void Start()
104  {
105  if (!isRunning)
106  {
107  startTimeStamp = GetTimestamp();
108  isRunning = true;
109  }
110  }
111 
114  [global::__DynamicallyInvokable]
115  public static Stopwatch StartNew()
116  {
117  Stopwatch stopwatch = new Stopwatch();
118  stopwatch.Start();
119  return stopwatch;
120  }
121 
123  [global::__DynamicallyInvokable]
124  public void Stop()
125  {
126  if (isRunning)
127  {
128  long timestamp = GetTimestamp();
129  long num = timestamp - startTimeStamp;
130  elapsed += num;
131  isRunning = false;
132  if (elapsed < 0)
133  {
134  elapsed = 0L;
135  }
136  }
137  }
138 
140  [global::__DynamicallyInvokable]
141  public void Reset()
142  {
143  elapsed = 0L;
144  isRunning = false;
145  startTimeStamp = 0L;
146  }
147 
149  [global::__DynamicallyInvokable]
150  public void Restart()
151  {
152  elapsed = 0L;
153  startTimeStamp = GetTimestamp();
154  isRunning = true;
155  }
156 
159  [global::__DynamicallyInvokable]
160  public static long GetTimestamp()
161  {
162  if (IsHighResolution)
163  {
164  long value = 0L;
165  Microsoft.Win32.SafeNativeMethods.QueryPerformanceCounter(out value);
166  return value;
167  }
168  return DateTime.UtcNow.Ticks;
169  }
170 
171  private long GetRawElapsedTicks()
172  {
173  long num = elapsed;
174  if (isRunning)
175  {
176  long timestamp = GetTimestamp();
177  long num2 = timestamp - startTimeStamp;
178  num += num2;
179  }
180  return num;
181  }
182 
183  private long GetElapsedDateTimeTicks()
184  {
185  long rawElapsedTicks = GetRawElapsedTicks();
186  if (IsHighResolution)
187  {
188  double num = rawElapsedTicks;
189  num *= tickFrequency;
190  return (long)num;
191  }
192  return rawElapsedTicks;
193  }
194  }
195 }
Stopwatch()
Initializes a new instance of the T:System.Diagnostics.Stopwatch class.
Definition: Stopwatch.cs:96
Provides a set of methods and properties that you can use to accurately measure elapsed time....
Definition: Stopwatch.cs:7
void Restart()
Stops time interval measurement, resets the elapsed time to zero, and starts measuring elapsed time.
Definition: Stopwatch.cs:150
long ElapsedTicks
Gets the total elapsed time measured by the current instance, in timer ticks.
Definition: Stopwatch.cs:70
void Stop()
Stops measuring elapsed time for an interval.
Definition: Stopwatch.cs:124
long ElapsedMilliseconds
Gets the total elapsed time measured by the current instance, in milliseconds.
Definition: Stopwatch.cs:58
static Stopwatch StartNew()
Initializes a new T:System.Diagnostics.Stopwatch instance, sets the elapsed time property to zero,...
Definition: Stopwatch.cs:115
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
Definition: DateTime.cs:13
static long GetTimestamp()
Gets the current number of ticks in the timer mechanism.
Definition: Stopwatch.cs:160
static readonly bool IsHighResolution
Indicates whether the timer is based on a high-resolution performance counter. This field is read-onl...
Definition: Stopwatch.cs:25
bool IsRunning
Gets a value indicating whether the T:System.Diagnostics.Stopwatch timer is running.
Definition: Stopwatch.cs:34
TimeSpan Elapsed
Gets the total elapsed time measured by the current instance.
Definition: Stopwatch.cs:46
static readonly long Frequency
Gets the frequency of the timer as the number of ticks per second. This field is read-only.
Definition: Stopwatch.cs:21
static DateTime UtcNow
Gets a T:System.DateTime object that is set to the current date and time on this computer,...
Definition: DateTime.cs:288
Represents a time interval.To browse the .NET Framework source code for this type,...
Definition: TimeSpan.cs:12
void Start()
Starts, or resumes, measuring elapsed time for an interval.
Definition: Stopwatch.cs:103
void Reset()
Stops time interval measurement and resets the elapsed time to zero.
Definition: Stopwatch.cs:141
long Ticks
Gets the number of ticks that represent the date and time of this instance.
Definition: DateTime.cs:315