Mercurial > pub > ImplabNet
diff Implab/FuncTaskBase.cs @ 192:f1da3afc3521 release v2.1
Слияние с v2
author | cin |
---|---|
date | Fri, 22 Apr 2016 13:10:34 +0300 |
parents | dd4a3590f9c6 |
children | 40d7fed4a09e |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/FuncTaskBase.cs Fri Apr 22 13:10:34 2016 +0300 @@ -0,0 +1,52 @@ +using System; + +namespace Implab { + public class FuncTaskBase<TResult> : AbstractTask<TResult> { + readonly Func<Exception, TResult> m_cancel; + readonly Func<Exception, TResult> m_error; + + protected FuncTaskBase( Func<Exception, TResult> error, Func<Exception, TResult> cancel, bool autoCancellable) { + m_error = error; + m_cancel = cancel; + if (autoCancellable) + CancellationRequested(CancelOperation); + } + + public void Reject(Exception error) { + Safe.ArgumentNotNull(error, "error"); + if (LockCancelation()) + HandleErrorInternal(error); + } + + protected void HandleErrorInternal(Exception error) { + if (m_error != null) { + try { + SetResult(m_error(error)); + } catch(Exception err) { + SetErrorInternal(err); + } + } else { + SetErrorInternal(error); + } + } + + public override void CancelOperation(Exception 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()); + } + } + + } +} +