Mercurial > pub > ImplabNet
comparison Implab/TaskController.cs @ 7:7ea9363fef6c promises
inital progress handling
author | cin |
---|---|
date | Fri, 01 Nov 2013 16:03:08 +0400 |
parents | |
children | aa33d0bb8c0c |
comparison
equal
deleted
inserted
replaced
6:dfa21d507bc5 | 7:7ea9363fef6c |
---|---|
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> | |
15 class TaskController | |
16 { | |
17 object m_lock; | |
18 string m_message; | |
19 bool m_cancelled; | |
20 | |
21 float m_current; | |
22 float m_max; | |
23 | |
24 public event EventHandler<ValueEventArgs<string>> MessageUpdated; | |
25 public event EventHandler<ValueEventArgs<float>> ProgressUpdated; | |
26 public event EventHandler<ProgressInitEventArgs> ProgressInit; | |
27 | |
28 public TaskController() | |
29 { | |
30 m_lock = new Object(); | |
31 } | |
32 | |
33 public string Message | |
34 { | |
35 get | |
36 { | |
37 lock (m_lock) | |
38 return m_message; | |
39 } | |
40 set | |
41 { | |
42 lock (m_lock) | |
43 { | |
44 m_message = value; | |
45 OnMessageUpdated(); | |
46 } | |
47 } | |
48 } | |
49 | |
50 public float CurrentProgress | |
51 { | |
52 get | |
53 { | |
54 lock (m_lock) | |
55 return m_current; | |
56 } | |
57 set | |
58 { | |
59 lock (m_lock) | |
60 { | |
61 var prev = m_current; | |
62 m_current = value; | |
63 if (m_current >= m_max) | |
64 m_current = m_max; | |
65 if (m_current != prev) | |
66 OnProgressUpdated(); | |
67 } | |
68 } | |
69 } | |
70 | |
71 public void InitProgress(float current, float max, string message) | |
72 { | |
73 if (max < 0) | |
74 throw new ArgumentOutOfRangeException("max"); | |
75 if (current < 0 || current > max) | |
76 throw new ArgumentOutOfRangeException("current"); | |
77 | |
78 lock(m_lock) { | |
79 m_current = current; | |
80 m_max = max; | |
81 m_message = message; | |
82 OnProgressInit(); | |
83 } | |
84 } | |
85 | |
86 protected virtual void OnMessageUpdated() | |
87 { | |
88 var temp = MessageUpdated; | |
89 if (temp != null) | |
90 { | |
91 temp(this, new ValueEventArgs<string>(m_message)); | |
92 } | |
93 } | |
94 | |
95 protected virtual void OnProgressUpdated() | |
96 { | |
97 var temp = ProgressUpdated; | |
98 if (temp != null) | |
99 { | |
100 temp(this,new ValueEventArgs<float>(m_current)); | |
101 } | |
102 } | |
103 | |
104 protected virtual void OnProgressInit() | |
105 { | |
106 var temp = ProgressInit; | |
107 if (temp != null) | |
108 { | |
109 temp(this, new ProgressInitEventArgs(m_current,m_max, m_message)); | |
110 } | |
111 } | |
112 } | |
113 } |