@@ -86,6 +86,13 @@ public abstract class TimerBase : IDisposable, INotifyPropertyChanged
86
86
/// </summary>
87
87
private TimeSpan ? timeLeft ;
88
88
89
+ /// <summary>
90
+ /// A <see cref="TimeSpan"/> representing the time since this timer has expired if the <see cref="State"/> is
91
+ /// <see cref="TimerState.Expired"/>, <see cref="TimeSpan.Zero"/> if the <see cref="State"/> is <see
92
+ /// cref="TimerState.Running"/> or <see cref="TimerState.Paused"/>, or <c>null</c> otherwise.
93
+ /// </summary>
94
+ private TimeSpan ? timeExpired ;
95
+
89
96
/// <summary>
90
97
/// A <see cref="TimeSpan"/> representing the total time that this timer will run for or has run for if the
91
98
/// <see cref="State"/> is <see cref="TimerState.Running"/> or <see cref="TimerState.Expired"/>, or <c>null</c>
@@ -129,6 +136,7 @@ protected TimerBase(TimerInfo timerInfo)
129
136
this . endTime = timerInfo . EndTime ;
130
137
this . timeElapsed = timerInfo . TimeElapsed ;
131
138
this . timeLeft = timerInfo . TimeLeft ;
139
+ this . timeExpired = timerInfo . TimeExpired ;
132
140
this . totalTime = timerInfo . TotalTime ;
133
141
134
142
if ( this . state == TimerState . Running )
@@ -227,6 +235,16 @@ public TimeSpan? TimeLeft
227
235
get { return this . timeLeft ; }
228
236
}
229
237
238
+ /// <summary>
239
+ /// Gets a <see cref="TimeSpan"/> representing the time since this timer has expired if the <see cref="State"/>
240
+ /// is <see cref="TimerState.Expired"/>, <see cref="TimeSpan.Zero"/> if the <see cref="State"/> is <see
241
+ /// cref="TimerState.Running"/> or <see cref="TimerState.Paused"/>, or <c>null</c> otherwise.
242
+ /// </summary>
243
+ public TimeSpan ? TimeExpired
244
+ {
245
+ get { return this . timeExpired ; }
246
+ }
247
+
230
248
/// <summary>
231
249
/// Gets a <see cref="TimeSpan"/> representing the total time that this timer will run for or has run for if
232
250
/// the <see cref="State"/> is <see cref="TimerState.Running"/> or <see cref="TimerState.Expired"/>, or
@@ -287,9 +305,10 @@ public virtual void Start(DateTime start, DateTime end)
287
305
this . endTime = end ;
288
306
this . timeElapsed = TimeSpan . Zero ;
289
307
this . timeLeft = this . endTime - this . startTime ;
308
+ this . timeExpired = TimeSpan . Zero ;
290
309
this . totalTime = this . timeLeft ;
291
310
292
- this . OnPropertyChanged ( "State" , "StartTime" , "EndTime" , "TimeElapsed" , "TimeLeft" , "TotalTime" ) ;
311
+ this . OnPropertyChanged ( "State" , "StartTime" , "EndTime" , "TimeElapsed" , "TimeLeft" , "TimeExpired" , " TotalTime") ;
293
312
this . OnStarted ( ) ;
294
313
295
314
this . Update ( ) ;
@@ -316,12 +335,13 @@ public virtual void Pause()
316
335
this . state = TimerState . Paused ;
317
336
this . timeElapsed = MathExtensions . Min ( now - ( this . startTime ?? now ) , this . totalTime ?? TimeSpan . Zero ) ;
318
337
this . timeLeft = MathExtensions . Max ( ( this . endTime ?? now ) - now , TimeSpan . Zero ) ;
338
+ this . timeExpired = TimeSpan . Zero ;
319
339
this . startTime = null ;
320
340
this . endTime = null ;
321
341
322
342
this . dispatcherTimer . Stop ( ) ;
323
343
324
- this . OnPropertyChanged ( "State" , "StartTime" , "EndTime" , "TimeElapsed" , "TimeLeft" ) ;
344
+ this . OnPropertyChanged ( "State" , "StartTime" , "EndTime" , "TimeElapsed" , "TimeExpired" , " TimeLeft") ;
325
345
this . OnPaused ( ) ;
326
346
}
327
347
@@ -373,11 +393,12 @@ public virtual void Stop()
373
393
this . endTime = null ;
374
394
this . timeElapsed = null ;
375
395
this . timeLeft = null ;
396
+ this . timeExpired = null ;
376
397
this . totalTime = null ;
377
398
378
399
this . dispatcherTimer . Stop ( ) ;
379
400
380
- this . OnPropertyChanged ( "State" , "StartTime" , "EndTime" , "TimeLeft" , "TotalTime" ) ;
401
+ this . OnPropertyChanged ( "State" , "StartTime" , "EndTime" , "TimeLeft" , "TimeExpired" , " TotalTime") ;
381
402
this . OnStopped ( ) ;
382
403
}
383
404
@@ -391,7 +412,7 @@ public virtual void Update()
391
412
{
392
413
this . ThrowIfDisposed ( ) ;
393
414
394
- if ( this . state != TimerState . Running )
415
+ if ( this . state != TimerState . Running && this . state != TimerState . Expired )
395
416
{
396
417
return ;
397
418
}
@@ -400,20 +421,20 @@ public virtual void Update()
400
421
DateTime now = DateTime . Now ;
401
422
this . timeElapsed = MathExtensions . Min ( now - ( this . startTime ?? now ) , this . totalTime ?? TimeSpan . Zero ) ;
402
423
this . timeLeft = MathExtensions . Max ( ( this . endTime ?? now ) - now , TimeSpan . Zero ) ;
424
+ this . timeExpired = MathExtensions . Max ( now - ( this . endTime ?? now ) , TimeSpan . Zero ) ;
403
425
404
- this . OnPropertyChanged ( "TimeElapsed" , "TimeLeft" ) ;
405
- this . OnTick ( ) ;
406
-
407
- // Check if the timer has expired
408
- if ( this . timeLeft <= TimeSpan . Zero )
426
+ // Raise an event when the timer expires
427
+ if ( this . timeLeft <= TimeSpan . Zero && this . state == TimerState . Running )
409
428
{
410
429
this . state = TimerState . Expired ;
411
430
412
- this . dispatcherTimer . Stop ( ) ;
413
-
414
431
this . OnPropertyChanged ( "State" ) ;
415
432
this . OnExpired ( ) ;
416
433
}
434
+
435
+ // Raise other events
436
+ this . OnPropertyChanged ( "TimeElapsed" , "TimeLeft" , "TimeExpired" ) ;
437
+ this . OnTick ( ) ;
417
438
}
418
439
419
440
/// <summary>
@@ -429,6 +450,7 @@ public virtual TimerInfo ToTimerInfo()
429
450
EndTime = this . endTime ,
430
451
TimeElapsed = this . timeElapsed ,
431
452
TimeLeft = this . timeLeft ,
453
+ TimeExpired = this . timeExpired ,
432
454
TotalTime = this . totalTime
433
455
} ;
434
456
}
0 commit comments