Mercurial > pub > ImplabNet
annotate Implab/TaskController.cs @ 230:3e26338eb977 v2
slowly cutting off mono specific settings
author | cin |
---|---|
date | Wed, 13 Sep 2017 16:55:13 +0300 |
parents | f75cfa58e3d4 |
children |
rev | line source |
---|---|
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 | |
76 | 95 public void Cancel() { |
12 | 96 lock (m_lock) { |
76 | 97 if (!m_cancelled) |
12 | 98 m_cancelled = true; |
99 } | |
100 } | |
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 | 109 protected virtual void OnCancelled() { |
110 var temp = Cancelled; | |
111 if (temp != null) { | |
112 temp(this,new EventArgs()); | |
113 } | |
114 } | |
115 | |
7 | 116 protected virtual void OnMessageUpdated() |
117 { | |
118 var temp = MessageUpdated; | |
119 if (temp != null) | |
120 { | |
121 temp(this, new ValueEventArgs<string>(m_message)); | |
122 } | |
123 } | |
124 | |
125 protected virtual void OnProgressUpdated() | |
126 { | |
127 var temp = ProgressUpdated; | |
128 if (temp != null) | |
129 { | |
130 temp(this,new ValueEventArgs<float>(m_current)); | |
131 } | |
132 } | |
133 | |
134 protected virtual void OnProgressInit() | |
135 { | |
136 var temp = ProgressInit; | |
137 if (temp != null) | |
138 { | |
139 temp(this, new ProgressInitEventArgs(m_current,m_max, m_message)); | |
140 } | |
141 } | |
142 } | |
143 } |