annotate Implab/TaskController.cs @ 162:0526412bbb26 ref20160224

DFA refactoring
author cin
date Wed, 24 Feb 2016 08:39:53 +0300
parents f75cfa58e3d4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
1 using System;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
2 using System.Collections.Generic;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
3 using System.Linq;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
4 using System.Text;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
5 using System.Threading;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
6
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
7 namespace Implab
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
8 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
9 /// <summary>
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
10 /// This class allows to interact with asyncronuos task.
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
11 /// </summary>
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
12 /// <remarks>
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
13 /// Members of this object are thread safe.
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
14 /// </remarks>
25
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
15 public class TaskController: IProgressNotifier, ITaskController
7
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
16 {
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 7
diff changeset
17 readonly object m_lock;
7
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
18 string m_message;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
19
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
20 float m_current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
21 float m_max;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
22
12
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
23 bool m_cancelled;
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
24
25
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
25 public event EventHandler Cancelled;
7
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
26 public event EventHandler<ValueEventArgs<string>> MessageUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
27 public event EventHandler<ValueEventArgs<float>> ProgressUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
28 public event EventHandler<ProgressInitEventArgs> ProgressInit;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
29
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
30 public TaskController()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
31 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
32 m_lock = new Object();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
33 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
34
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
35 public string Message
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
36 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
37 get
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
38 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
39 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
40 return m_message;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
41 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
42 set
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
43 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
44 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
45 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
46 m_message = value;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
47 OnMessageUpdated();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
48 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
49 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
50 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
51
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
52 public float CurrentProgress
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
53 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
54 get
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
55 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
56 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
57 return m_current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
58 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
59 set
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
60 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
61 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
62 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
63 var prev = m_current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
64 m_current = value;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
65 if (m_current >= m_max)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
66 m_current = m_max;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
67 if (m_current != prev)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
68 OnProgressUpdated();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
69 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
70 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
71 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
72
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
73 public void InitProgress(float current, float max, string message)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
74 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
75 if (max < 0)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
76 throw new ArgumentOutOfRangeException("max");
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
77 if (current < 0 || current > max)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
78 throw new ArgumentOutOfRangeException("current");
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
79
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
80 lock(m_lock) {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
81 m_current = current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
82 m_max = max;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
83 m_message = message;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
84 OnProgressInit();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
85 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
86 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
87
25
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
88 public bool IsCancelled {
12
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
89 get {
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
90 lock (m_lock)
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
91 return m_cancelled;
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
92 }
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
93 }
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
94
76
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 25
diff changeset
95 public void Cancel() {
12
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
96 lock (m_lock) {
76
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 25
diff changeset
97 if (!m_cancelled)
12
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
98 m_cancelled = true;
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
99 }
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
100 }
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
101
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 76
diff changeset
102 public void Cancel(Exception reason) {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 76
diff changeset
103 lock (m_lock) {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 76
diff changeset
104 if (!m_cancelled)
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 76
diff changeset
105 m_cancelled = true;
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 76
diff changeset
106 }
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 76
diff changeset
107 }
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 76
diff changeset
108
25
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
109 protected virtual void OnCancelled() {
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
110 var temp = Cancelled;
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
111 if (temp != null) {
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
112 temp(this,new EventArgs());
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
113 }
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
114 }
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
115
7
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
116 protected virtual void OnMessageUpdated()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
117 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
118 var temp = MessageUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
119 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
120 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
121 temp(this, new ValueEventArgs<string>(m_message));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
122 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
123 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
124
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
125 protected virtual void OnProgressUpdated()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
126 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
127 var temp = ProgressUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
128 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
129 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
130 temp(this,new ValueEventArgs<float>(m_current));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
131 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
132 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
133
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
134 protected virtual void OnProgressInit()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
135 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
136 var temp = ProgressInit;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
137 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
138 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
139 temp(this, new ProgressInitEventArgs(m_current,m_max, m_message));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
140 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
141 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
142 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
143 }