annotate Implab/TaskController.cs @ 8:849075f49d5c promises

sync
author cin
date Fri, 01 Nov 2013 16:03:52 +0400
parents 7ea9363fef6c
children aa33d0bb8c0c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
1 using System;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
2 using System.Collections.Generic;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
3 using System.Linq;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
4 using System.Text;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
5 using System.Threading;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
6
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
7 namespace Implab
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
8 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
9 /// <summary>
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
10 /// This class allows to interact with asyncronuos task.
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
11 /// </summary>
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
12 /// <remarks>
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
13 /// Members of this object are thread safe.
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
14 /// </remarks>
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
15 class TaskController
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
16 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
17 object m_lock;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
18 string m_message;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
19 bool m_cancelled;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
20
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
21 float m_current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
22 float m_max;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
23
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
24 public event EventHandler<ValueEventArgs<string>> MessageUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
25 public event EventHandler<ValueEventArgs<float>> ProgressUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
26 public event EventHandler<ProgressInitEventArgs> ProgressInit;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
27
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
28 public TaskController()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
29 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
30 m_lock = new Object();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
31 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
32
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
33 public string Message
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
34 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
35 get
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
36 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
37 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
38 return m_message;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
39 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
40 set
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
41 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
42 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
43 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
44 m_message = value;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
45 OnMessageUpdated();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
46 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
47 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
48 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
49
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
50 public float CurrentProgress
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
51 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
52 get
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
53 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
54 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
55 return m_current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
56 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
57 set
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
58 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
59 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
60 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
61 var prev = m_current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
62 m_current = value;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
63 if (m_current >= m_max)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
64 m_current = m_max;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
65 if (m_current != prev)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
66 OnProgressUpdated();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
67 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
68 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
69 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
70
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
71 public void InitProgress(float current, float max, string message)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
72 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
73 if (max < 0)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
74 throw new ArgumentOutOfRangeException("max");
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
75 if (current < 0 || current > max)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
76 throw new ArgumentOutOfRangeException("current");
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
77
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
78 lock(m_lock) {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
79 m_current = current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
80 m_max = max;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
81 m_message = message;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
82 OnProgressInit();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
83 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
84 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
85
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
86 protected virtual void OnMessageUpdated()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
87 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
88 var temp = MessageUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
89 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
90 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
91 temp(this, new ValueEventArgs<string>(m_message));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
92 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
93 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
94
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
95 protected virtual void OnProgressUpdated()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
96 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
97 var temp = ProgressUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
98 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
99 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
100 temp(this,new ValueEventArgs<float>(m_current));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
101 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
102 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
103
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
104 protected virtual void OnProgressInit()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
105 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
106 var temp = ProgressInit;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
107 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
108 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
109 temp(this, new ProgressInitEventArgs(m_current,m_max, m_message));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
110 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
111 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
112 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
113 }