ActionTaskBase.cs
53 lines
| 1.6 KiB
| text/x-csharp
|
CSharpLexer
/ Implab / ActionTaskBase.cs
cin
|
r145 | using System; | ||
namespace Implab { | ||||
cin
|
r187 | public class ActionTaskBase : AbstractTask { | ||
cin
|
r145 | readonly Action<Exception> m_cancel; | ||
readonly Action<Exception> m_error; | ||||
cin
|
r149 | protected ActionTaskBase( Action<Exception> error, Action<Exception> cancel, bool autoCancellable) { | ||
cin
|
r145 | m_error = error; | ||
m_cancel = cancel; | ||||
cin
|
r149 | if (autoCancellable) | ||
CancellationRequested(CancelOperation); | ||||
cin
|
r145 | } | ||
public void Reject(Exception error) { | ||||
Safe.ArgumentNotNull(error, "error"); | ||||
if (LockCancelation()) | ||||
HandleErrorInternal(error); | ||||
} | ||||
cin
|
r187 | public override void CancelOperation(Exception reason) { | ||
if (LockCancelation()) | ||||
HandleCancelInternal(reason); | ||||
} | ||||
cin
|
r145 | protected void HandleErrorInternal(Exception error) { | ||
if (m_error != null) { | ||||
try { | ||||
m_error(error); | ||||
SetResult(); | ||||
} catch(Exception err) { | ||||
cin
|
r187 | SetErrorInternal(err); | ||
cin
|
r145 | } | ||
} else { | ||||
cin
|
r187 | SetErrorInternal(error); | ||
cin
|
r145 | } | ||
} | ||||
cin
|
r187 | protected void HandleCancelInternal(Exception error) { | ||
if (m_cancel != null) { | ||||
try { | ||||
m_cancel(error); | ||||
SetResult(); | ||||
} catch(Exception err) { | ||||
HandleErrorInternal(err); | ||||
cin
|
r145 | } | ||
cin
|
r187 | } else { | ||
HandleErrorInternal(error ?? new OperationCanceledException()); | ||||
cin
|
r145 | } | ||
} | ||||
} | ||||
} | ||||