annotate Implab/Components/PollingComponent.cs @ 260:547a2fc0d93e v3 v3.0.6

minor fixes
author cin
date Fri, 13 Apr 2018 19:14:59 +0300
parents 7d52dc684bbd
children f1696cdc3d7a
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
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
12 Task m_pending;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
13 Task m_poll;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
14
258
d0876436d95d missing file
cin
parents:
diff changeset
15 /// <summary>
d0876436d95d missing file
cin
parents:
diff changeset
16 /// Poll interval in milliseconds.
d0876436d95d missing file
cin
parents:
diff changeset
17 /// </summary>
d0876436d95d missing file
cin
parents:
diff changeset
18 /// <returns></returns>
d0876436d95d missing file
cin
parents:
diff changeset
19 public int Interval { get; set; }
d0876436d95d missing file
cin
parents:
diff changeset
20
d0876436d95d missing file
cin
parents:
diff changeset
21 /// <summary>
d0876436d95d missing file
cin
parents:
diff changeset
22 /// Delay to the first poll after start in milliseconds
d0876436d95d missing file
cin
parents:
diff changeset
23 /// </summary>
d0876436d95d missing file
cin
parents:
diff changeset
24 /// <returns></returns>
d0876436d95d missing file
cin
parents:
diff changeset
25 public int Delay { get; set; }
d0876436d95d missing file
cin
parents:
diff changeset
26
d0876436d95d missing file
cin
parents:
diff changeset
27 /// <summary>
d0876436d95d missing file
cin
parents:
diff changeset
28 /// Indicates how to handle unhandled exceptions in <see cref="Poll()"/> method.
d0876436d95d missing file
cin
parents:
diff changeset
29 /// </summary>
d0876436d95d missing file
cin
parents:
diff changeset
30 /// <returns></returns>
d0876436d95d missing file
cin
parents:
diff changeset
31 public bool FailOnError { get; set; }
d0876436d95d missing file
cin
parents:
diff changeset
32
d0876436d95d missing file
cin
parents:
diff changeset
33 /// <summary>
d0876436d95d missing file
cin
parents:
diff changeset
34 /// Event for the unhandled exceptions in <see cref="Poll()"/> method.
d0876436d95d missing file
cin
parents:
diff changeset
35 /// </summary>
d0876436d95d missing file
cin
parents:
diff changeset
36 public event EventHandler<UnhandledExceptionEventArgs> UnhandledException;
d0876436d95d missing file
cin
parents:
diff changeset
37
d0876436d95d missing file
cin
parents:
diff changeset
38 protected PollingComponent(bool initialized) : base(initialized) {
d0876436d95d missing file
cin
parents:
diff changeset
39 m_timer = new Timer(OnTimer);
d0876436d95d missing file
cin
parents:
diff changeset
40 }
d0876436d95d missing file
cin
parents:
diff changeset
41
d0876436d95d missing file
cin
parents:
diff changeset
42 protected override void RunInternal() {
d0876436d95d missing file
cin
parents:
diff changeset
43 ScheduleNextPoll(Delay);
d0876436d95d missing file
cin
parents:
diff changeset
44 }
d0876436d95d missing file
cin
parents:
diff changeset
45
d0876436d95d missing file
cin
parents:
diff changeset
46
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
47 protected override async Task StopInternalAsync(CancellationToken ct) {
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
48 // component in Stopping state, no new polls will be scheduled
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
49 m_cancellation.Cancel();
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
50 try {
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
51 // await for pending poll
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
52 await m_poll;
260
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
53 } catch (OperationCanceledException) {
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
54 // OK
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
55 }
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
56 }
258
d0876436d95d missing file
cin
parents:
diff changeset
57
d0876436d95d missing file
cin
parents:
diff changeset
58 protected abstract Task Poll(CancellationToken ct);
d0876436d95d missing file
cin
parents:
diff changeset
59
d0876436d95d missing file
cin
parents:
diff changeset
60 void ScheduleNextPoll(int timeout) {
d0876436d95d missing file
cin
parents:
diff changeset
61 lock (SynchronizationObject) {
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
62 if (State == ExecutionState.Running) {
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
63 m_pending = Safe.CreateTask(m_cancellation.Token);
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
64 m_poll = m_pending.Then(() => Poll(m_cancellation.Token));
258
d0876436d95d missing file
cin
parents:
diff changeset
65 m_timer.Change(timeout, Timeout.Infinite);
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
66 }
258
d0876436d95d missing file
cin
parents:
diff changeset
67 }
d0876436d95d missing file
cin
parents:
diff changeset
68 }
d0876436d95d missing file
cin
parents:
diff changeset
69
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
70 async void OnTimer(object state) {
258
d0876436d95d missing file
cin
parents:
diff changeset
71 try {
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
72 m_pending.Start();
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
73 await m_poll;
260
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
74 ScheduleNextPoll(Interval);
258
d0876436d95d missing file
cin
parents:
diff changeset
75 } catch (Exception e) {
d0876436d95d missing file
cin
parents:
diff changeset
76 UnhandledException.DispatchEvent(this, new UnhandledExceptionEventArgs(e, false));
260
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
77
258
d0876436d95d missing file
cin
parents:
diff changeset
78 if (FailOnError)
d0876436d95d missing file
cin
parents:
diff changeset
79 Fail(e);
260
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
80 else
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
81 ScheduleNextPoll(Interval);
258
d0876436d95d missing file
cin
parents:
diff changeset
82 }
260
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
83
258
d0876436d95d missing file
cin
parents:
diff changeset
84 }
d0876436d95d missing file
cin
parents:
diff changeset
85
d0876436d95d missing file
cin
parents:
diff changeset
86 protected override void Dispose(bool disposing) {
d0876436d95d missing file
cin
parents:
diff changeset
87 if (disposing)
d0876436d95d missing file
cin
parents:
diff changeset
88 Safe.Dispose(m_timer, m_cancellation);
d0876436d95d missing file
cin
parents:
diff changeset
89 base.Dispose(disposing);
d0876436d95d missing file
cin
parents:
diff changeset
90 }
d0876436d95d missing file
cin
parents:
diff changeset
91
d0876436d95d missing file
cin
parents:
diff changeset
92 }
d0876436d95d missing file
cin
parents:
diff changeset
93 }