comparison Implab/ActionChainTaskBase.cs @ 186:75103928da09 ref20160224

working on cancelation and error handling
author cin
date Tue, 19 Apr 2016 00:50:14 +0300
parents eb793fbbe4ea
children dd4a3590f9c6
comparison
equal deleted inserted replaced
185:822aab37b107 186:75103928da09
18 public void Reject(Exception error) { 18 public void Reject(Exception error) {
19 if (LockCancelation()) 19 if (LockCancelation())
20 HandleErrorInternal(error); 20 HandleErrorInternal(error);
21 } 21 }
22 22
23
24
25 public override void CancelOperation(Exception reason) { 23 public override void CancelOperation(Exception reason) {
26 if (LockCancelation()) { 24 if (LockCancelation()) {
25 if (!(reason is OperationCanceledException))
26 reason = reason != null ? new OperationCanceledException(null, reason) : new OperationCanceledException();
27
27 if (m_cancel != null) { 28 if (m_cancel != null) {
28 try { 29 try {
29 m_cancel(reason).On(SetResult, SetError, SetCancelled); 30 m_cancel(reason).On(SetResult, HandleErrorInternal, HandleCancelInternal);
30 } catch (Exception err) { 31 } catch (Exception err) {
31 HandleErrorInternal(err); 32 HandleErrorInternal(err);
32 } 33 }
33 } else { 34 } else {
34 SetCancelled(reason); 35 HandleErrorInternal(reason);
35 } 36 }
36 } 37 }
37 } 38 }
38 39
39 protected void HandleErrorInternal(Exception error) { 40 void HandleCancelInternal(Exception reason) {
41 if (!(reason is OperationCanceledException))
42 reason = reason != null ? new OperationCanceledException(null, reason) : new OperationCanceledException();
43 HandleErrorInternal(reason);
44 }
45
46 void HandleErrorInternal(Exception error) {
40 if (m_error != null) { 47 if (m_error != null) {
41 try { 48 try {
42 var p = m_error(error); 49 var p = m_error(error);
43 p.On(SetResult,SetError,SetCancelled); 50 p.On(SetResult, SetError, SetCancelled);
44 CancellationRequested(p.Cancel); 51 CancellationRequested(p.Cancel);
45 } catch (Exception err) { 52 } catch (Exception err) {
46 SetError(err); 53 error = err;
47 } 54 }
48 } else { 55 } else {
56 SetErrorInternal(error);
57 }
58 }
59
60 void SetErrorInternal(Exception error) {
61 while (error is PromiseTransientException)
62 error = error.InnerException;
63
64 if (error is OperationCanceledException)
65 SetCancelled(error);
66 else
49 SetError(error); 67 SetError(error);
50 }
51 } 68 }
52 69
53 protected bool LockCancelation() { 70 protected bool LockCancelation() {
54 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0); 71 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
55 } 72 }