annotate Implab/Components/PollingComponent.cs @ 258:d0876436d95d v3

missing file
author cin
date Fri, 13 Apr 2018 00:44:57 +0300
parents
children 7d52dc684bbd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
258
d0876436d95d missing file
cin
parents:
diff changeset
1 using System;
d0876436d95d missing file
cin
parents:
diff changeset
2 using System.Threading;
d0876436d95d missing file
cin
parents:
diff changeset
3 using System.Threading.Tasks;
d0876436d95d missing file
cin
parents:
diff changeset
4
d0876436d95d missing file
cin
parents:
diff changeset
5 namespace Implab.Components {
d0876436d95d missing file
cin
parents:
diff changeset
6 public abstract class PollingComponent : RunnableComponent {
d0876436d95d missing file
cin
parents:
diff changeset
7
d0876436d95d missing file
cin
parents:
diff changeset
8 readonly Timer m_timer;
d0876436d95d missing file
cin
parents:
diff changeset
9
d0876436d95d missing file
cin
parents:
diff changeset
10 readonly CancellationTokenSource m_cancellation = new CancellationTokenSource();
d0876436d95d missing file
cin
parents:
diff changeset
11
d0876436d95d missing file
cin
parents:
diff changeset
12 /// <summary>
d0876436d95d missing file
cin
parents:
diff changeset
13 /// Poll interval in milliseconds.
d0876436d95d missing file
cin
parents:
diff changeset
14 /// </summary>
d0876436d95d missing file
cin
parents:
diff changeset
15 /// <returns></returns>
d0876436d95d missing file
cin
parents:
diff changeset
16 public int Interval { get; set; }
d0876436d95d missing file
cin
parents:
diff changeset
17
d0876436d95d missing file
cin
parents:
diff changeset
18 /// <summary>
d0876436d95d missing file
cin
parents:
diff changeset
19 /// Delay to the first poll after start in milliseconds
d0876436d95d missing file
cin
parents:
diff changeset
20 /// </summary>
d0876436d95d missing file
cin
parents:
diff changeset
21 /// <returns></returns>
d0876436d95d missing file
cin
parents:
diff changeset
22 public int Delay { get; set; }
d0876436d95d missing file
cin
parents:
diff changeset
23
d0876436d95d missing file
cin
parents:
diff changeset
24 /// <summary>
d0876436d95d missing file
cin
parents:
diff changeset
25 /// Indicates how to handle unhandled exceptions in <see cref="Poll()"/> method.
d0876436d95d missing file
cin
parents:
diff changeset
26 /// </summary>
d0876436d95d missing file
cin
parents:
diff changeset
27 /// <returns></returns>
d0876436d95d missing file
cin
parents:
diff changeset
28 public bool FailOnError { get; set; }
d0876436d95d missing file
cin
parents:
diff changeset
29
d0876436d95d missing file
cin
parents:
diff changeset
30 /// <summary>
d0876436d95d missing file
cin
parents:
diff changeset
31 /// Event for the unhandled exceptions in <see cref="Poll()"/> method.
d0876436d95d missing file
cin
parents:
diff changeset
32 /// </summary>
d0876436d95d missing file
cin
parents:
diff changeset
33 public event EventHandler<UnhandledExceptionEventArgs> UnhandledException;
d0876436d95d missing file
cin
parents:
diff changeset
34
d0876436d95d missing file
cin
parents:
diff changeset
35 protected PollingComponent(bool initialized) : base(initialized) {
d0876436d95d missing file
cin
parents:
diff changeset
36 m_timer = new Timer(OnTimer);
d0876436d95d missing file
cin
parents:
diff changeset
37 }
d0876436d95d missing file
cin
parents:
diff changeset
38
d0876436d95d missing file
cin
parents:
diff changeset
39 protected override void RunInternal() {
d0876436d95d missing file
cin
parents:
diff changeset
40 ScheduleNextPoll(Delay);
d0876436d95d missing file
cin
parents:
diff changeset
41 }
d0876436d95d missing file
cin
parents:
diff changeset
42
d0876436d95d missing file
cin
parents:
diff changeset
43
d0876436d95d missing file
cin
parents:
diff changeset
44 //TODO override stop
d0876436d95d missing file
cin
parents:
diff changeset
45
d0876436d95d missing file
cin
parents:
diff changeset
46 protected abstract Task Poll(CancellationToken ct);
d0876436d95d missing file
cin
parents:
diff changeset
47
d0876436d95d missing file
cin
parents:
diff changeset
48 void ScheduleNextPoll(int timeout) {
d0876436d95d missing file
cin
parents:
diff changeset
49 lock (SynchronizationObject) {
d0876436d95d missing file
cin
parents:
diff changeset
50 if (State == ExecutionState.Running)
d0876436d95d missing file
cin
parents:
diff changeset
51 m_timer.Change(timeout, Timeout.Infinite);
d0876436d95d missing file
cin
parents:
diff changeset
52 }
d0876436d95d missing file
cin
parents:
diff changeset
53 }
d0876436d95d missing file
cin
parents:
diff changeset
54
d0876436d95d missing file
cin
parents:
diff changeset
55 void OnTimer(object state) {
d0876436d95d missing file
cin
parents:
diff changeset
56 try {
d0876436d95d missing file
cin
parents:
diff changeset
57 Poll(m_cancellation.Token);
d0876436d95d missing file
cin
parents:
diff changeset
58 } catch (Exception e) {
d0876436d95d missing file
cin
parents:
diff changeset
59 UnhandledException.DispatchEvent(this, new UnhandledExceptionEventArgs(e, false));
d0876436d95d missing file
cin
parents:
diff changeset
60 if (FailOnError)
d0876436d95d missing file
cin
parents:
diff changeset
61 Fail(e);
d0876436d95d missing file
cin
parents:
diff changeset
62 }
d0876436d95d missing file
cin
parents:
diff changeset
63 ScheduleNextPoll(Interval);
d0876436d95d missing file
cin
parents:
diff changeset
64 }
d0876436d95d missing file
cin
parents:
diff changeset
65
d0876436d95d missing file
cin
parents:
diff changeset
66 protected override void Dispose(bool disposing) {
d0876436d95d missing file
cin
parents:
diff changeset
67 if (disposing)
d0876436d95d missing file
cin
parents:
diff changeset
68 Safe.Dispose(m_timer, m_cancellation);
d0876436d95d missing file
cin
parents:
diff changeset
69 base.Dispose(disposing);
d0876436d95d missing file
cin
parents:
diff changeset
70 }
d0876436d95d missing file
cin
parents:
diff changeset
71
d0876436d95d missing file
cin
parents:
diff changeset
72 }
d0876436d95d missing file
cin
parents:
diff changeset
73 }