FuncTaskBase.cs
52 lines
| 1.6 KiB
| text/x-csharp
|
CSharpLexer
/ Implab / FuncTaskBase.cs
cin
|
r144 | using System; | ||
namespace Implab { | ||||
cin
|
r187 | public class FuncTaskBase<TResult> : AbstractTask<TResult> { | ||
cin
|
r144 | readonly Func<Exception, TResult> m_cancel; | ||
readonly Func<Exception, TResult> m_error; | ||||
cin
|
r149 | protected FuncTaskBase( Func<Exception, TResult> error, Func<Exception, TResult> cancel, bool autoCancellable) { | ||
cin
|
r144 | m_error = error; | ||
m_cancel = cancel; | ||||
cin
|
r149 | if (autoCancellable) | ||
CancellationRequested(CancelOperation); | ||||
cin
|
r144 | } | ||
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) { | ||||
cin
|
r187 | SetErrorInternal(err); | ||
cin
|
r144 | } | ||
} else { | ||||
cin
|
r187 | SetErrorInternal(error); | ||
cin
|
r144 | } | ||
} | ||||
public override void CancelOperation(Exception reason) { | ||||
cin
|
r187 | if (LockCancelation()) | ||
HandleCancelInternal(reason); | ||||
} | ||||
protected void HandleCancelInternal(Exception reason) { | ||||
if (m_cancel != null) { | ||||
try { | ||||
SetResult(m_cancel(reason)); | ||||
} catch (Exception err) { | ||||
HandleErrorInternal(err); | ||||
cin
|
r144 | } | ||
cin
|
r187 | } else { | ||
HandleErrorInternal(reason ?? new OperationCanceledException()); | ||||
cin
|
r144 | } | ||
} | ||||
} | ||||
} | ||||