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
|
149
|
11 protected ActionTaskBase( Action<Exception> error, Action<Exception> cancel, bool autoCancellable) {
|
145
|
12 m_error = error;
|
|
13 m_cancel = cancel;
|
149
|
14 if (autoCancellable)
|
|
15 CancellationRequested(CancelOperation);
|
145
|
16 }
|
|
17
|
|
18 public void Reject(Exception error) {
|
|
19 Safe.ArgumentNotNull(error, "error");
|
|
20 if (LockCancelation())
|
|
21 HandleErrorInternal(error);
|
|
22 }
|
|
23
|
|
24 protected void HandleErrorInternal(Exception error) {
|
|
25 if (m_error != null) {
|
|
26 try {
|
|
27 m_error(error);
|
|
28 SetResult();
|
|
29 } catch(Exception err) {
|
|
30 SetError(err);
|
|
31 }
|
|
32 } else {
|
|
33 SetError(error);
|
|
34 }
|
|
35 }
|
|
36
|
|
37 public override void CancelOperation(Exception reason) {
|
|
38 if (LockCancelation()) {
|
|
39 if (m_cancel != null) {
|
|
40 try {
|
|
41 m_cancel(reason);
|
|
42 SetResult();
|
|
43 } catch (Exception err) {
|
|
44 HandleErrorInternal(err);
|
|
45 }
|
|
46 } else {
|
|
47 SetCancelled(reason);
|
|
48 }
|
|
49 }
|
|
50 }
|
|
51
|
|
52 protected bool LockCancelation() {
|
|
53 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
|
|
54 }
|
|
55 }
|
|
56 }
|
|
57
|