##// END OF EJS Templates
runnable component, work in progress
runnable component, work in progress

File last commit:

r149:eb793fbbe4ea v2
r185:822aab37b107 ref20160224
Show More
ActionTaskBase.cs
57 lines | 1.6 KiB | text/x-csharp | CSharpLexer
cin
RC: cancellation support for promises + tests
r145 using System;
using System.Threading;
namespace Implab {
public class ActionTaskBase : AbstractPromise {
readonly Action<Exception> m_cancel;
readonly Action<Exception> m_error;
int m_cancelationLock;
cin
fixed promises cancellation
r149 protected ActionTaskBase( Action<Exception> error, Action<Exception> cancel, bool autoCancellable) {
cin
RC: cancellation support for promises + tests
r145 m_error = error;
m_cancel = cancel;
cin
fixed promises cancellation
r149 if (autoCancellable)
CancellationRequested(CancelOperation);
cin
RC: cancellation support for promises + tests
r145 }
public void Reject(Exception error) {
Safe.ArgumentNotNull(error, "error");
if (LockCancelation())
HandleErrorInternal(error);
}
protected void HandleErrorInternal(Exception error) {
if (m_error != null) {
try {
m_error(error);
SetResult();
} catch(Exception err) {
SetError(err);
}
} else {
SetError(error);
}
}
public override void CancelOperation(Exception reason) {
if (LockCancelation()) {
if (m_cancel != null) {
try {
m_cancel(reason);
SetResult();
} catch (Exception err) {
HandleErrorInternal(err);
}
} else {
SetCancelled(reason);
}
}
}
protected bool LockCancelation() {
return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
}
}
}