annotate Implab/Components/PollingComponent.cs @ 274:22629bf26121 v3

Unity xml configuration, alpha2
author cin
date Fri, 27 Apr 2018 04:47:52 +0300
parents f1696cdc3d7a
children
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
262
f1696cdc3d7a Added IInitializable.Initialize() overload
cin
parents: 260
diff changeset
52 if (m_poll != null)
f1696cdc3d7a Added IInitializable.Initialize() overload
cin
parents: 260
diff changeset
53 await m_poll;
260
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
54 } catch (OperationCanceledException) {
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
55 // OK
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
56 }
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
57 }
258
d0876436d95d missing file
cin
parents:
diff changeset
58
d0876436d95d missing file
cin
parents:
diff changeset
59 protected abstract Task Poll(CancellationToken ct);
d0876436d95d missing file
cin
parents:
diff changeset
60
d0876436d95d missing file
cin
parents:
diff changeset
61 void ScheduleNextPoll(int timeout) {
d0876436d95d missing file
cin
parents:
diff changeset
62 lock (SynchronizationObject) {
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
63 if (State == ExecutionState.Running) {
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
64 m_pending = Safe.CreateTask(m_cancellation.Token);
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
65 m_poll = m_pending.Then(() => Poll(m_cancellation.Token));
258
d0876436d95d missing file
cin
parents:
diff changeset
66 m_timer.Change(timeout, Timeout.Infinite);
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
67 }
258
d0876436d95d missing file
cin
parents:
diff changeset
68 }
d0876436d95d missing file
cin
parents:
diff changeset
69 }
d0876436d95d missing file
cin
parents:
diff changeset
70
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
71 async void OnTimer(object state) {
258
d0876436d95d missing file
cin
parents:
diff changeset
72 try {
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
73 m_pending.Start();
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents: 258
diff changeset
74 await m_poll;
260
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
75 ScheduleNextPoll(Interval);
258
d0876436d95d missing file
cin
parents:
diff changeset
76 } catch (Exception e) {
d0876436d95d missing file
cin
parents:
diff changeset
77 UnhandledException.DispatchEvent(this, new UnhandledExceptionEventArgs(e, false));
260
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
78
258
d0876436d95d missing file
cin
parents:
diff changeset
79 if (FailOnError)
d0876436d95d missing file
cin
parents:
diff changeset
80 Fail(e);
260
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
81 else
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
82 ScheduleNextPoll(Interval);
258
d0876436d95d missing file
cin
parents:
diff changeset
83 }
260
547a2fc0d93e minor fixes
cin
parents: 259
diff changeset
84
258
d0876436d95d missing file
cin
parents:
diff changeset
85 }
d0876436d95d missing file
cin
parents:
diff changeset
86
d0876436d95d missing file
cin
parents:
diff changeset
87 protected override void Dispose(bool disposing) {
d0876436d95d missing file
cin
parents:
diff changeset
88 if (disposing)
d0876436d95d missing file
cin
parents:
diff changeset
89 Safe.Dispose(m_timer, m_cancellation);
d0876436d95d missing file
cin
parents:
diff changeset
90 base.Dispose(disposing);
d0876436d95d missing file
cin
parents:
diff changeset
91 }
d0876436d95d missing file
cin
parents:
diff changeset
92
d0876436d95d missing file
cin
parents:
diff changeset
93 }
d0876436d95d missing file
cin
parents:
diff changeset
94 }