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