annotate Implab/TaskController.cs @ 77:91362ffbecf8 v2

ported tests to mono
author cin
date Thu, 11 Sep 2014 10:56:14 +0400
parents c761fc982e1d
children f75cfa58e3d4
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
25
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
102 protected virtual void OnCancelled() {
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
103 var temp = Cancelled;
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
104 if (temp != null) {
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
105 temp(this,new EventArgs());
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
106 }
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
107 }
9bf5b23650c9 refactoring
cin
parents: 12
diff changeset
108
7
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
109 protected virtual void OnMessageUpdated()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
110 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
111 var temp = MessageUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
112 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
113 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
114 temp(this, new ValueEventArgs<string>(m_message));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
115 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
116 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
117
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
118 protected virtual void OnProgressUpdated()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
119 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
120 var temp = ProgressUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
121 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
122 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
123 temp(this,new ValueEventArgs<float>(m_current));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
124 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
125 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
126
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
127 protected virtual void OnProgressInit()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
128 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
129 var temp = ProgressInit;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
130 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
131 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
132 temp(this, new ProgressInitEventArgs(m_current,m_max, m_message));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
133 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
134 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
135 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
136 }