Skip to content

Commit 9ba5485

Browse files
committed
Use TimeSpan in Timers
1 parent dea950f commit 9ba5485

File tree

5 files changed

+66
-44
lines changed

5 files changed

+66
-44
lines changed

Raspberry.System/Timers/HighResolutionTimer.cs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class HighResolutionTimer : ITimer
1515
{
1616
#region Fields
1717

18-
private decimal delay;
19-
private decimal interval;
18+
private TimeSpan delay;
19+
private TimeSpan interval;
2020
private Action action;
2121

2222
private Thread thread;
@@ -41,17 +41,17 @@ public HighResolutionTimer()
4141
#region Properties
4242

4343
/// <summary>
44-
/// Gets or sets the interval, in milliseconds.
44+
/// Gets or sets the interval.
4545
/// </summary>
4646
/// <value>
47-
/// The interval, in milliseconds.
47+
/// The interval.
4848
/// </value>
49-
public decimal Interval
49+
public TimeSpan Interval
5050
{
5151
get { return interval; }
5252
set
5353
{
54-
if (value > uint.MaxValue/1000)
54+
if (value.TotalMilliseconds > uint.MaxValue/1000)
5555
throw new ArgumentOutOfRangeException("value", interval, "Interval must be lower than or equal to uint.MaxValue / 1000");
5656

5757
interval = value;
@@ -84,35 +84,36 @@ public Action Action
8484
/// Sleeps the specified delay.
8585
/// </summary>
8686
/// <param name="delay">The delay.</param>
87-
public static void Sleep(decimal delay)
87+
public static void Sleep(TimeSpan delay)
8888
{
8989
// Based on [BCM2835 C library](http://www.open.com.au/mikem/bcm2835/)
9090

9191
// Calling nanosleep() takes at least 100-200 us, so use it for
9292
// 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;
9494

95-
if (delay >= 100)
95+
var millisecondDelay = (decimal)delay.TotalMilliseconds;
96+
if (millisecondDelay >= 100)
9697
{
9798
// Do not use high resolution timer for long interval (>= 100ms)
98-
Thread.Sleep((int) delay);
99+
Thread.Sleep(delay);
99100
}
100-
else if (delay > 0.450m)
101+
else if (millisecondDelay > 0.450m)
101102
{
102103
var t1 = new Interop.timespec();
103104
var t2 = new Interop.timespec();
104105

105106
// Use nanosleep if interval is higher than 450µs
106107
t1.tv_sec = (IntPtr)0;
107-
t1.tv_nsec = (IntPtr)((long) (delay * 1000000) - nanoSleepOffset);
108+
t1.tv_nsec = (IntPtr)((long) (millisecondDelay * 1000000) - nanoSleepOffset);
108109

109110
Interop.nanosleep(ref t1, ref t2);
110111
}
111112
else
112113
{
113114
while (true)
114115
{
115-
if ((DateTime.Now.Ticks - start) * 0.0001m >= delay)
116+
if ((DateTime.UtcNow.Ticks - start) * 0.0001m >= millisecondDelay)
116117
break;
117118
}
118119
}
@@ -123,19 +124,19 @@ public static void Sleep(decimal delay)
123124
/// Starts this instance.
124125
/// </summary>
125126
/// <param name="startDelay">The delay before the first occurence, in milliseconds.</param>
126-
public void Start(decimal startDelay)
127+
public void Start(TimeSpan startDelay)
127128
{
128-
if (startDelay > uint.MaxValue/1000)
129+
if (startDelay.TotalMilliseconds > uint.MaxValue/1000)
129130
throw new ArgumentOutOfRangeException("startDelay", startDelay, "Delay must be lower than or equal to uint.MaxValue / 1000");
130131

131132
lock (this)
132133
{
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();
139140
}
140141
}
141142

@@ -146,12 +147,12 @@ public void Stop()
146147
{
147148
lock (this)
148149
{
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;
155156
}
156157
}
157158

@@ -173,10 +174,10 @@ private static int Calibrate()
173174
t1.tv_sec = (IntPtr) 0;
174175
t1.tv_nsec = (IntPtr) 1000000;
175176

176-
var start = DateTime.Now.Ticks;
177+
var start = DateTime.UtcNow.Ticks;
177178
Interop.nanosleep(ref t1, ref t2);
178179

179-
return a + ((DateTime.Now.Ticks - start) * 100 - 1000000);
180+
return a + ((DateTime.UtcNow.Ticks - start) * 100 - 1000000);
180181
},
181182
a => (int)(a / referenceCount));
182183
}

Raspberry.System/Timers/ITimer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public interface ITimer
1414
#region Properties
1515

1616
/// <summary>
17-
/// Gets or sets the interval, in milliseconds.
17+
/// Gets or sets the interval.
1818
/// </summary>
1919
/// <value>
20-
/// The interval, in milliseconds.
20+
/// The interval.
2121
/// </value>
22-
decimal Interval { get; set; }
22+
TimeSpan Interval { get; set; }
2323

2424
/// <summary>
2525
/// Gets or sets the action.
@@ -36,8 +36,8 @@ public interface ITimer
3636
/// <summary>
3737
/// Starts this instance.
3838
/// </summary>
39-
/// <param name="startDelay">The delay before the first occurence, in milliseconds.</param>
40-
void Start(decimal startDelay);
39+
/// <param name="startDelay">The delay before the first occurence.</param>
40+
void Start(TimeSpan startDelay);
4141

4242
/// <summary>
4343
/// Stops this instance.

Raspberry.System/Timers/StandardTimer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class StandardTimer : ITimer
1313
{
1414
#region Fields
1515

16-
private decimal interval;
16+
private TimeSpan interval;
1717
private Action action;
1818

1919
private bool isStarted;
@@ -29,14 +29,14 @@ public class StandardTimer : ITimer
2929
/// <value>
3030
/// The interval, in milliseconds.
3131
/// </value>
32-
public decimal Interval
32+
public TimeSpan Interval
3333
{
3434
get { return interval; }
3535
set
3636
{
3737
interval = value;
3838
if (isStarted)
39-
Start(0);
39+
Start(TimeSpan.Zero);
4040
}
4141
}
4242

@@ -66,14 +66,14 @@ public Action Action
6666
/// Starts this instance.
6767
/// </summary>
6868
/// <param name="startDelay">The delay before the first occurence, in milliseconds.</param>
69-
public void Start(decimal startDelay)
69+
public void Start(TimeSpan startDelay)
7070
{
7171
lock (this)
7272
{
73-
if (!isStarted && interval >= 1.0m)
73+
if (!isStarted && interval.TotalMilliseconds >= 1)
7474
{
7575
isStarted = true;
76-
timer = new System.Threading.Timer(OnElapsed, null, (int) startDelay, (int) interval);
76+
timer = new System.Threading.Timer(OnElapsed, null, startDelay, interval);
7777
}
7878
else
7979
Stop();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace Raspberry.Timers
4+
{
5+
/// <summary>
6+
/// Provides utilities for <see cref="TimeSpan"/>.
7+
/// </summary>
8+
public static class TimeSpanUtility
9+
{
10+
/// <summary>
11+
/// Creates a timespan from a number of microseconds.
12+
/// </summary>
13+
/// <param name="microseconds">The microseconds.</param>
14+
/// <returns></returns>
15+
public static TimeSpan FromMicroseconds(double microseconds)
16+
{
17+
return TimeSpan.FromTicks((long)(microseconds/10));
18+
}
19+
}
20+
}

Raspberry.System/Timers/Timer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#region References
22

3+
using System;
34
using System.Threading;
45

56
#endregion
@@ -30,16 +31,16 @@ public static ITimer Create()
3031
/// <summary>
3132
/// Sleeps during the specified time.
3233
/// </summary>
33-
/// <param name="time">The time, in milliseconds.</param>
34-
public static void Sleep(decimal time)
34+
/// <param name="time">The time.</param>
35+
public static void Sleep(TimeSpan time)
3536
{
36-
if (time < 0)
37+
if (time.TotalMilliseconds < 0)
3738
return;
3839

3940
if (Board.Current.IsRaspberryPi)
4041
HighResolutionTimer.Sleep(time);
4142
else
42-
Thread.Sleep((int)time);
43+
Thread.Sleep(time);
4344
}
4445

4546
#endregion

0 commit comments

Comments
 (0)