##// END OF EJS Templates
sync
sync

File last commit:

r149:eb793fbbe4ea v2
r168:8fb9c9507a26 ref20160224
Show More
FuncTaskBase.cs
55 lines | 1.6 KiB | text/x-csharp | CSharpLexer
cin
DRAFT: refactoring
r144 using System;
using System.Threading;
namespace Implab {
public class FuncTaskBase<TResult> : AbstractPromise<TResult> {
readonly Func<Exception, TResult> m_cancel;
readonly Func<Exception, TResult> m_error;
int m_cancelationLock;
cin
fixed promises cancellation
r149 protected FuncTaskBase( Func<Exception, TResult> error, Func<Exception, TResult> cancel, bool autoCancellable) {
cin
DRAFT: refactoring
r144 m_error = error;
m_cancel = cancel;
cin
fixed promises cancellation
r149 if (autoCancellable)
CancellationRequested(CancelOperation);
cin
DRAFT: refactoring
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) {
SetError(err);
}
} else {
SetError(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);
}
}
}
protected bool LockCancelation() {
return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
}
}
}