Mercurial > pub > ImplabNet
comparison Implab/ActionTaskBase.cs @ 187:dd4a3590f9c6 ref20160224
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
Any unhandled OperationCanceledException will cause the promise cancelation
| author | cin |
|---|---|
| date | Tue, 19 Apr 2016 17:35:20 +0300 |
| parents | eb793fbbe4ea |
| children | 40d7fed4a09e |
comparison
equal
deleted
inserted
replaced
| 186:75103928da09 | 187:dd4a3590f9c6 |
|---|---|
| 1 using System; | 1 using System; |
| 2 using System.Threading; | |
| 3 | 2 |
| 4 namespace Implab { | 3 namespace Implab { |
| 5 public class ActionTaskBase : AbstractPromise { | 4 public class ActionTaskBase : AbstractTask { |
| 6 readonly Action<Exception> m_cancel; | 5 readonly Action<Exception> m_cancel; |
| 7 readonly Action<Exception> m_error; | 6 readonly Action<Exception> m_error; |
| 8 | |
| 9 int m_cancelationLock; | |
| 10 | 7 |
| 11 protected ActionTaskBase( Action<Exception> error, Action<Exception> cancel, bool autoCancellable) { | 8 protected ActionTaskBase( Action<Exception> error, Action<Exception> cancel, bool autoCancellable) { |
| 12 m_error = error; | 9 m_error = error; |
| 13 m_cancel = cancel; | 10 m_cancel = cancel; |
| 14 if (autoCancellable) | 11 if (autoCancellable) |
| 19 Safe.ArgumentNotNull(error, "error"); | 16 Safe.ArgumentNotNull(error, "error"); |
| 20 if (LockCancelation()) | 17 if (LockCancelation()) |
| 21 HandleErrorInternal(error); | 18 HandleErrorInternal(error); |
| 22 } | 19 } |
| 23 | 20 |
| 21 public override void CancelOperation(Exception reason) { | |
| 22 if (LockCancelation()) | |
| 23 HandleCancelInternal(reason); | |
| 24 } | |
| 25 | |
| 24 protected void HandleErrorInternal(Exception error) { | 26 protected void HandleErrorInternal(Exception error) { |
| 25 if (m_error != null) { | 27 if (m_error != null) { |
| 26 try { | 28 try { |
| 27 m_error(error); | 29 m_error(error); |
| 28 SetResult(); | 30 SetResult(); |
| 29 } catch(Exception err) { | 31 } catch(Exception err) { |
| 30 SetError(err); | 32 SetErrorInternal(err); |
| 31 } | 33 } |
| 32 } else { | 34 } else { |
| 33 SetError(error); | 35 SetErrorInternal(error); |
| 34 } | 36 } |
| 35 } | 37 } |
| 36 | 38 |
| 37 public override void CancelOperation(Exception reason) { | 39 protected void HandleCancelInternal(Exception error) { |
| 38 if (LockCancelation()) { | 40 if (m_cancel != null) { |
| 39 if (m_cancel != null) { | 41 try { |
| 40 try { | 42 m_cancel(error); |
| 41 m_cancel(reason); | 43 SetResult(); |
| 42 SetResult(); | 44 } catch(Exception err) { |
| 43 } catch (Exception err) { | 45 HandleErrorInternal(err); |
| 44 HandleErrorInternal(err); | |
| 45 } | |
| 46 } else { | |
| 47 SetCancelled(reason); | |
| 48 } | 46 } |
| 47 } else { | |
| 48 HandleErrorInternal(error ?? new OperationCanceledException()); | |
| 49 } | 49 } |
| 50 } | |
| 51 | |
| 52 protected bool LockCancelation() { | |
| 53 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0); | |
| 54 } | 50 } |
| 55 } | 51 } |
| 56 } | 52 } |
| 57 | 53 |
