annotate Implab/Components/PollingComponent.cs @ 259:7d52dc684bbd v3

PollingComponent: implemented correct stopping
author cin
date Fri, 13 Apr 2018 03:57:39 +0300
parents d0876436d95d
children 547a2fc0d93e
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;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
53 } catch (OperationCanceledException e) {
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;
258
d0876436d95d missing file
cin
parents:
diff changeset
74 } catch (Exception e) {
d0876436d95d missing file
cin
parents:
diff changeset
75 UnhandledException.DispatchEvent(this, new UnhandledExceptionEventArgs(e, false));
d0876436d95d missing file
cin
parents:
diff changeset
76 if (FailOnError)
d0876436d95d missing file
cin
parents:
diff changeset
77 Fail(e);
d0876436d95d missing file
cin
parents:
diff changeset
78 }
d0876436d95d missing file
cin
parents:
diff changeset
79 ScheduleNextPoll(Interval);
d0876436d95d missing file
cin
parents:
diff changeset
80 }
d0876436d95d missing file
cin
parents:
diff changeset
81
d0876436d95d missing file
cin
parents:
diff changeset
82 protected override void Dispose(bool disposing) {
d0876436d95d missing file
cin
parents:
diff changeset
83 if (disposing)
d0876436d95d missing file
cin
parents:
diff changeset
84 Safe.Dispose(m_timer, m_cancellation);
d0876436d95d missing file
cin
parents:
diff changeset
85 base.Dispose(disposing);
d0876436d95d missing file
cin
parents:
diff changeset
86 }
d0876436d95d missing file
cin
parents:
diff changeset
87
d0876436d95d missing file
cin
parents:
diff changeset
88 }
d0876436d95d missing file
cin
parents:
diff changeset
89 }