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