Mercurial > pub > ImplabNet
diff Implab/FuncTaskBase.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/FuncTaskBase.cs Tue Apr 19 00:50:14 2016 +0300 +++ b/Implab/FuncTaskBase.cs Tue Apr 19 17:35:20 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); - } } }