diff Implab/FuncTaskBase.cs @ 190:1c2a16d071a7 v2

Слияние с ref20160224
author cin
date Fri, 22 Apr 2016 13:08:08 +0300
parents dd4a3590f9c6
children 40d7fed4a09e
line wrap: on
line diff
--- a/Implab/FuncTaskBase.cs	Fri Feb 19 18:07:17 2016 +0300
+++ b/Implab/FuncTaskBase.cs	Fri Apr 22 13:08:08 2016 +0300
@@ -1,13 +1,10 @@
 using System;
-using System.Threading;
 
 namespace Implab {
-    public class FuncTaskBase<TResult> : AbstractPromise<TResult> {
+    public class FuncTaskBase<TResult> : AbstractTask<TResult> {
         readonly Func<Exception, TResult> m_cancel;
         readonly Func<Exception, TResult> m_error;
 
-        int m_cancelationLock;
-
         protected FuncTaskBase( Func<Exception, TResult> error, Func<Exception, TResult> cancel, bool autoCancellable) {
             m_error = error;
             m_cancel = cancel;
@@ -26,30 +23,30 @@
                 try {
                     SetResult(m_error(error));
                 } 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 {
-                        SetResult(m_cancel(reason));
-                    } catch (Exception err) {
-                        HandleErrorInternal(err);
-                    }
-                } else {
-                    SetCancelled(reason);
+            if (LockCancelation())
+                HandleCancelInternal(reason);
+        }
+
+        protected void HandleCancelInternal(Exception reason) {
+            if (m_cancel != null) {
+                try {
+                    SetResult(m_cancel(reason));
+                } catch (Exception err) {
+                    HandleErrorInternal(err);
                 }
+            } else {
+                HandleErrorInternal(reason ?? new OperationCanceledException());
             }
         }
 
-        protected bool LockCancelation() {
-            return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
-        }
     }
 }