diff 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
line wrap: on
line diff
--- a/Implab/ActionTaskBase.cs	Tue Apr 19 00:50:14 2016 +0300
+++ b/Implab/ActionTaskBase.cs	Tue Apr 19 17:35:20 2016 +0300
@@ -1,13 +1,10 @@
 using System;
-using System.Threading;
 
 namespace Implab {
-    public class ActionTaskBase : AbstractPromise {
+    public class ActionTaskBase : AbstractTask {
         readonly Action<Exception> m_cancel;
         readonly Action<Exception> m_error;
 
-        int m_cancelationLock;
-
         protected ActionTaskBase( Action<Exception> error, Action<Exception> cancel, bool autoCancellable) {
             m_error = error;
             m_cancel = cancel;
@@ -21,37 +18,36 @@
                 HandleErrorInternal(error);
         }
 
+        public override void CancelOperation(Exception reason) {
+            if (LockCancelation())
+                HandleCancelInternal(reason);
+        }
+
         protected void HandleErrorInternal(Exception error) {
             if (m_error != null) {
                 try {
                     m_error(error);
                     SetResult();
                 } catch(Exception err) {
-                    SetError(err);
+                    SetErrorInternal(err);
                 }
             } else {
-                SetError(error);
+                SetErrorInternal(error);
             }
         }
 
-        public override void CancelOperation(Exception reason) {
-            if (LockCancelation()) {
-                if (m_cancel != null) {
-                    try {
-                        m_cancel(reason);
-                        SetResult();
-                    } catch (Exception err) {
-                        HandleErrorInternal(err);
-                    }
-                } else {
-                    SetCancelled(reason);
+        protected void HandleCancelInternal(Exception error) {
+            if (m_cancel != null) {
+                try {
+                    m_cancel(error);
+                    SetResult();
+                } catch(Exception err) {
+                    HandleErrorInternal(err);
                 }
+            } else {
+                HandleErrorInternal(error ?? new OperationCanceledException());
             }
         }
-
-        protected bool LockCancelation() {
-            return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
-        }
     }
 }