145
|
1 using System;
|
|
2 using System.Threading;
|
|
3
|
|
4 namespace Implab {
|
|
5 public class ActionTaskBase : AbstractPromise {
|
|
6 readonly Action<Exception> m_cancel;
|
|
7 readonly Action<Exception> m_error;
|
|
8
|
|
9 int m_cancelationLock;
|
|
10
|
|
11 protected ActionTaskBase( Action<Exception> error, Action<Exception> cancel) {
|
|
12 m_error = error;
|
|
13 m_cancel = cancel;
|
|
14 }
|
|
15
|
|
16 public void Reject(Exception error) {
|
|
17 Safe.ArgumentNotNull(error, "error");
|
|
18 if (LockCancelation())
|
|
19 HandleErrorInternal(error);
|
|
20 }
|
|
21
|
|
22 protected void HandleErrorInternal(Exception error) {
|
|
23 if (m_error != null) {
|
|
24 try {
|
|
25 m_error(error);
|
|
26 SetResult();
|
|
27 } catch(Exception err) {
|
|
28 SetError(err);
|
|
29 }
|
|
30 } else {
|
|
31 SetError(error);
|
|
32 }
|
|
33 }
|
|
34
|
|
35 public override void CancelOperation(Exception reason) {
|
|
36 if (LockCancelation()) {
|
|
37 if (m_cancel != null) {
|
|
38 try {
|
|
39 m_cancel(reason);
|
|
40 SetResult();
|
|
41 } catch (Exception err) {
|
|
42 HandleErrorInternal(err);
|
|
43 }
|
|
44 } else {
|
|
45 SetCancelled(reason);
|
|
46 }
|
|
47 }
|
|
48 }
|
|
49
|
|
50 protected bool LockCancelation() {
|
|
51 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
|
|
52 }
|
|
53 }
|
|
54 }
|
|
55
|