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