wasSharp – Blame information for rev 27

Subversion Repositories:
Rev:
Rev Author Line No. Line
10 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
8 using System.Threading;
9 using System.Threading.Tasks;
10  
11 namespace wasSharp.Timers
12 {
13 public class Timer : IDisposable
14 {
20 office 15 private readonly Task CompletedTask = Task.FromResult(false);
10 office 16 private Task Delay;
17 private bool Disposed;
21 office 18  
20 office 19 private CancellationTokenSource tokenSource;
10 office 20  
21 office 21 public Timer()
10 office 22 {
21 office 23 }
20 office 24  
21 office 25 public Timer(Action callback, TimeSpan dueTime, TimeSpan period) : this()
26 {
27 Callback = callback;
28 DueTime = dueTime;
29 Period = period;
30  
31 Start();
10 office 32 }
33  
20 office 34 public Timer(Action callback, int dueTime, int period)
35 : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period))
10 office 36 {
37 }
38  
20 office 39 public Timer(Action callback) : this(callback, TimeSpan.Zero, TimeSpan.Zero)
10 office 40 {
41 }
42  
20 office 43 public Timer(Action callback, double dueTime, int period)
44 : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period))
10 office 45 {
46 }
47  
20 office 48 public Timer(Action callback, uint dueTime, uint period)
49 : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period))
10 office 50 {
51 }
52  
21 office 53 public Action Callback { set; get; }
54  
55 public TimeSpan DueTime { get; set; } = TimeSpan.Zero;
56  
57 public TimeSpan Period { get; set; } = TimeSpan.Zero;
58  
25 office 59 public DateTime ScheduledTime
60 {
61 get;
62 set;
63 }
64  
20 office 65 public void Dispose()
10 office 66 {
21 office 67 // Stop the timer.
68 Stop();
69  
70 // Dispose the token.
20 office 71 if (tokenSource != null)
72 {
73 tokenSource.Cancel();
74 tokenSource.Dispose();
75 tokenSource = null;
76 }
21 office 77  
78 // Set the disposed flag.
10 office 79 Disposed = true;
80 }
81  
21 office 82 private void Start()
10 office 83 {
21 office 84 // Check if we have an installed callback and that there is at least a due time.
85 if (Callback == null || DueTime.Equals(TimeSpan.Zero))
86 return;
87  
88 // Dispose the previous token source.
20 office 89 if (tokenSource != null)
90 {
91 tokenSource.Cancel();
92 tokenSource.Dispose();
93 tokenSource = null;
94 }
10 office 95  
20 office 96 // Create a new cancellation source.
97 tokenSource = new CancellationTokenSource();
10 office 98  
99 Action tick = null;
21 office 100  
10 office 101 tick = () =>
102 {
21 office 103 Task.Run(Callback, tokenSource.Token);
10 office 104 if (Disposed)
105 return;
25 office 106 switch (!Period.Equals(TimeSpan.Zero))
107 {
108 case true:
109 ScheduledTime = DateTime.UtcNow;
110 Delay = Task.Delay(DueTime, tokenSource.Token);
111 break;
27 office 112  
25 office 113 default:
114 Delay = CompletedTask;
115 break;
116 }
21 office 117 if (Disposed || Delay.IsCompleted)
10 office 118 return;
21 office 119 Delay.ContinueWith(o => tick(), tokenSource.Token);
10 office 120 };
20 office 121  
25 office 122 switch (!DueTime.Equals(TimeSpan.Zero))
123 {
124 case true:
125 ScheduledTime = DateTime.UtcNow;
126 Delay = Task.Delay(DueTime, tokenSource.Token);
127 break;
27 office 128  
25 office 129 default:
130 Delay = CompletedTask;
131 break;
132 }
21 office 133 if (Disposed || Delay.IsCompleted)
10 office 134 return;
21 office 135 Delay.ContinueWith(o => tick(), tokenSource.Token);
10 office 136 }
137  
20 office 138 public void Change(int dueTime, int period)
10 office 139 {
20 office 140 Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period));
10 office 141 }
142  
20 office 143 public void Change(uint dueTime, int period)
10 office 144 {
20 office 145 Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period));
10 office 146 }
20 office 147  
148 public void Change(TimeSpan dueTime, TimeSpan period)
149 {
21 office 150 DueTime = dueTime;
151 Period = period;
20 office 152  
21 office 153 Start();
20 office 154 }
155  
156 public void Stop()
157 {
158 Change(0, 0);
159 }
10 office 160 }
27 office 161 }