annotate Implab/TaskController.cs @ 10:aa33d0bb8c0c promises

implemeted new cancellable promises concept
author cin
date Sun, 03 Nov 2013 18:07:38 +0400
parents 7ea9363fef6c
children eb418ba8275b
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 {
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 7
diff changeset
17 readonly object m_lock;
7
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
18 string m_message;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
19
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
20 float m_current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
21 float m_max;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
22
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
23 public event EventHandler<ValueEventArgs<string>> MessageUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
24 public event EventHandler<ValueEventArgs<float>> ProgressUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
25 public event EventHandler<ProgressInitEventArgs> ProgressInit;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
26
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
27 public TaskController()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
28 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
29 m_lock = new Object();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
30 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
31
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
32 public string Message
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
33 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
34 get
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
35 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
36 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
37 return m_message;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
38 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
39 set
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
40 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
41 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
42 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
43 m_message = value;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
44 OnMessageUpdated();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
45 }
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 public float CurrentProgress
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
50 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
51 get
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
52 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
53 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
54 return m_current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
55 }
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
56 set
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
57 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
58 lock (m_lock)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
59 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
60 var prev = m_current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
61 m_current = value;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
62 if (m_current >= m_max)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
63 m_current = m_max;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
64 if (m_current != prev)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
65 OnProgressUpdated();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
66 }
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 public void InitProgress(float current, float max, string message)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
71 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
72 if (max < 0)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
73 throw new ArgumentOutOfRangeException("max");
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
74 if (current < 0 || current > max)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
75 throw new ArgumentOutOfRangeException("current");
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
76
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
77 lock(m_lock) {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
78 m_current = current;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
79 m_max = max;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
80 m_message = message;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
81 OnProgressInit();
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
82 }
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 protected virtual void OnMessageUpdated()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
86 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
87 var temp = MessageUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
88 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
89 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
90 temp(this, new ValueEventArgs<string>(m_message));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
91 }
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 protected virtual void OnProgressUpdated()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
95 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
96 var temp = ProgressUpdated;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
97 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
98 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
99 temp(this,new ValueEventArgs<float>(m_current));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
100 }
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 protected virtual void OnProgressInit()
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
104 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
105 var temp = ProgressInit;
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
106 if (temp != null)
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
107 {
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
108 temp(this, new ProgressInitEventArgs(m_current,m_max, m_message));
7ea9363fef6c inital progress handling
cin
parents:
diff changeset
109 }
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 }