@@ -15,8 +15,8 @@ public class HighResolutionTimer : ITimer
15
15
{
16
16
#region Fields
17
17
18
- private decimal delay ;
19
- private decimal interval ;
18
+ private TimeSpan delay ;
19
+ private TimeSpan interval ;
20
20
private Action action ;
21
21
22
22
private Thread thread ;
@@ -41,17 +41,17 @@ public HighResolutionTimer()
41
41
#region Properties
42
42
43
43
/// <summary>
44
- /// Gets or sets the interval, in milliseconds .
44
+ /// Gets or sets the interval.
45
45
/// </summary>
46
46
/// <value>
47
- /// The interval, in milliseconds .
47
+ /// The interval.
48
48
/// </value>
49
- public decimal Interval
49
+ public TimeSpan Interval
50
50
{
51
51
get { return interval ; }
52
52
set
53
53
{
54
- if ( value > uint . MaxValue / 1000 )
54
+ if ( value . TotalMilliseconds > uint . MaxValue / 1000 )
55
55
throw new ArgumentOutOfRangeException ( "value" , interval , "Interval must be lower than or equal to uint.MaxValue / 1000" ) ;
56
56
57
57
interval = value ;
@@ -84,35 +84,36 @@ public Action Action
84
84
/// Sleeps the specified delay.
85
85
/// </summary>
86
86
/// <param name="delay">The delay.</param>
87
- public static void Sleep ( decimal delay )
87
+ public static void Sleep ( TimeSpan delay )
88
88
{
89
89
// Based on [BCM2835 C library](http://www.open.com.au/mikem/bcm2835/)
90
90
91
91
// Calling nanosleep() takes at least 100-200 us, so use it for
92
92
// long waits and use a busy wait on the hires timer for the rest.
93
- var start = DateTime . Now . Ticks ;
93
+ var start = DateTime . UtcNow . Ticks ;
94
94
95
- if ( delay >= 100 )
95
+ var millisecondDelay = ( decimal ) delay . TotalMilliseconds ;
96
+ if ( millisecondDelay >= 100 )
96
97
{
97
98
// Do not use high resolution timer for long interval (>= 100ms)
98
- Thread . Sleep ( ( int ) delay ) ;
99
+ Thread . Sleep ( delay ) ;
99
100
}
100
- else if ( delay > 0.450m )
101
+ else if ( millisecondDelay > 0.450m )
101
102
{
102
103
var t1 = new Interop . timespec ( ) ;
103
104
var t2 = new Interop . timespec ( ) ;
104
105
105
106
// Use nanosleep if interval is higher than 450µs
106
107
t1 . tv_sec = ( IntPtr ) 0 ;
107
- t1 . tv_nsec = ( IntPtr ) ( ( long ) ( delay * 1000000 ) - nanoSleepOffset ) ;
108
+ t1 . tv_nsec = ( IntPtr ) ( ( long ) ( millisecondDelay * 1000000 ) - nanoSleepOffset ) ;
108
109
109
110
Interop . nanosleep ( ref t1 , ref t2 ) ;
110
111
}
111
112
else
112
113
{
113
114
while ( true )
114
115
{
115
- if ( ( DateTime . Now . Ticks - start ) * 0.0001m >= delay )
116
+ if ( ( DateTime . UtcNow . Ticks - start ) * 0.0001m >= millisecondDelay )
116
117
break ;
117
118
}
118
119
}
@@ -123,19 +124,19 @@ public static void Sleep(decimal delay)
123
124
/// Starts this instance.
124
125
/// </summary>
125
126
/// <param name="startDelay">The delay before the first occurence, in milliseconds.</param>
126
- public void Start ( decimal startDelay )
127
+ public void Start ( TimeSpan startDelay )
127
128
{
128
- if ( startDelay > uint . MaxValue / 1000 )
129
+ if ( startDelay . TotalMilliseconds > uint . MaxValue / 1000 )
129
130
throw new ArgumentOutOfRangeException ( "startDelay" , startDelay , "Delay must be lower than or equal to uint.MaxValue / 1000" ) ;
130
131
131
132
lock ( this )
132
133
{
133
- if ( thread == null )
134
- {
135
- delay = startDelay ;
136
- thread = new Thread ( ThreadProcess ) ;
137
- thread . Start ( ) ;
138
- }
134
+ if ( thread != null )
135
+ return ;
136
+
137
+ delay = startDelay ;
138
+ thread = new Thread ( ThreadProcess ) ;
139
+ thread . Start ( ) ;
139
140
}
140
141
}
141
142
@@ -146,12 +147,12 @@ public void Stop()
146
147
{
147
148
lock ( this )
148
149
{
149
- if ( thread != null )
150
- {
151
- if ( thread != Thread . CurrentThread )
152
- thread . Abort ( ) ;
153
- thread = null ;
154
- }
150
+ if ( thread == null )
151
+ return ;
152
+
153
+ if ( thread != Thread . CurrentThread )
154
+ thread . Abort ( ) ;
155
+ thread = null ;
155
156
}
156
157
}
157
158
@@ -173,10 +174,10 @@ private static int Calibrate()
173
174
t1 . tv_sec = ( IntPtr ) 0 ;
174
175
t1 . tv_nsec = ( IntPtr ) 1000000 ;
175
176
176
- var start = DateTime . Now . Ticks ;
177
+ var start = DateTime . UtcNow . Ticks ;
177
178
Interop . nanosleep ( ref t1 , ref t2 ) ;
178
179
179
- return a + ( ( DateTime . Now . Ticks - start ) * 100 - 1000000 ) ;
180
+ return a + ( ( DateTime . UtcNow . Ticks - start ) * 100 - 1000000 ) ;
180
181
} ,
181
182
a => ( int ) ( a / referenceCount ) ) ;
182
183
}
0 commit comments