main
1using System;
2using System.Collections.Generic;
3
4namespace jive
5{
6 public class IntervalTimer : Timer
7 {
8 readonly ITimerFactory factory;
9 readonly IDictionary<TimerClient, System.Timers.Timer> timers;
10
11 public IntervalTimer() : this(new TimerFactory())
12 {
13 }
14
15 public IntervalTimer(ITimerFactory factory)
16 {
17 this.factory = factory;
18 timers = new Dictionary<TimerClient, System.Timers.Timer>();
19 }
20
21 public void start_notifying(TimerClient client_to_be_notified, TimeSpan span)
22 {
23 stop_notifying(client_to_be_notified);
24
25 var timer = factory.create_for(span);
26 timer.Elapsed += (o, e) =>
27 {
28 client_to_be_notified.notify();
29 };
30 timer.Start();
31 timers[client_to_be_notified] = timer;
32 }
33
34 public void stop_notifying(TimerClient client_to_stop_notifying)
35 {
36 if (!timers.ContainsKey(client_to_stop_notifying)) return;
37 timers[client_to_stop_notifying].Stop();
38 timers[client_to_stop_notifying].Dispose();
39 }
40
41 public void Dispose()
42 {
43 timers.each(x => x.Value.Dispose());
44 timers.Clear();
45 }
46 }
47}