annotate Implab/TaskController.cs @ 12:eb418ba8275b promises

refactoring, added WorkerPool
author cin
date Tue, 05 Nov 2013 19:55:34 +0400
parents aa33d0bb8c0c
children 9bf5b23650c9
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>
12
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
15 class TaskController: IProgressNotifier, ITaskController, ICancellable
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
7
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
25 public event EventHandler<ValueEventArgs<string>> MessageUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
26 public event EventHandler<ValueEventArgs<float>> ProgressUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
27 public event EventHandler<ProgressInitEventArgs> ProgressInit;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
28
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
29 public TaskController()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
30 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
31 m_lock = new Object();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
32 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
33
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
34 public string Message
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
35 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
36 get
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
37 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
38 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
39 return m_message;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
40 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
41 set
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
42 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
43 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
44 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
45 m_message = value;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
46 OnMessageUpdated();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
47 }
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 public float CurrentProgress
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
52 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
53 get
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
54 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
55 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
56 return m_current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
57 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
58 set
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
59 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
60 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
61 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
62 var prev = m_current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
63 m_current = value;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
64 if (m_current >= m_max)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
65 m_current = m_max;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
66 if (m_current != prev)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
67 OnProgressUpdated();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
68 }
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 public void InitProgress(float current, float max, string message)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
73 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
74 if (max < 0)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
75 throw new ArgumentOutOfRangeException("max");
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
76 if (current < 0 || current > max)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
77 throw new ArgumentOutOfRangeException("current");
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
78
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
79 lock(m_lock) {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
80 m_current = current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
81 m_max = max;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
82 m_message = message;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
83 OnProgressInit();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
84 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
85 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
86
12
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
87 public bool Cancelled {
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
88 get {
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
89 lock (m_lock)
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
90 return m_cancelled;
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
91 }
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 public bool Cancel() {
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
95 lock (m_lock) {
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
96 if (!m_cancelled) {
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
97 m_cancelled = true;
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
98 return true;
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
99 } else {
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
100 return false;
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
101 }
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
102 }
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
103 }
eb418ba8275b refactoring, added WorkerPool
cin
parents: 10
diff changeset
104
7
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
105 protected virtual void OnMessageUpdated()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
106 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
107 var temp = MessageUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
108 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
109 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
110 temp(this, new ValueEventArgs<string>(m_message));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
111 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
112 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
113
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
114 protected virtual void OnProgressUpdated()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
115 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
116 var temp = ProgressUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
117 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
118 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
119 temp(this,new ValueEventArgs<float>(m_current));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
120 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
121 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
122
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
123 protected virtual void OnProgressInit()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
124 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
125 var temp = ProgressInit;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
126 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
127 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
128 temp(this, new ProgressInitEventArgs(m_current,m_max, m_message));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
129 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
130 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
131 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
132 }