annotate Implab/FuncChainTaskBase.cs @ 187:dd4a3590f9c6 ref20160224

Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler Any unhandled OperationCanceledException will cause the promise cancelation
author cin
date Tue, 19 Apr 2016 17:35:20 +0300
parents eb793fbbe4ea
children 40d7fed4a09e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
1 using System;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
2
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
3 namespace Implab {
187
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
4 public class FuncChainTaskBase<TResult> : AbstractTask<TResult> {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
5 readonly Func<Exception, IPromise<TResult>> m_error;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
6 readonly Func<Exception, IPromise<TResult>> m_cancel;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
7
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
8 protected FuncChainTaskBase( Func<Exception, IPromise<TResult>> error, Func<Exception, IPromise<TResult>> cancel, bool autoCancellable) {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
9 m_error = error;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
10 m_cancel = cancel;
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
11 if (autoCancellable)
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
12 CancellationRequested(CancelOperation);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
13 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
14
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
15 public void Reject(Exception error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
16 if (LockCancelation())
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
17 HandleErrorInternal(error);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
18 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
19
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
20 public override void CancelOperation(Exception reason) {
187
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
21 if (LockCancelation())
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
22 HandleCancelInternal(reason);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
23 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
24
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
25 protected void HandleErrorInternal(Exception error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
26 if (m_error != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
27 try {
187
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
28 var p = m_error(error);
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
29 p.On(SetResult, SetErrorInternal, SetCancelledInternal);
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
30 CancellationRequested(p.Cancel);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
31 } catch(Exception err) {
187
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
32 SetErrorInternal(err);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
33 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
34 } else {
187
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
35 SetErrorInternal(error);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
36 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
37 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
38
187
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
39 protected void HandleCancelInternal(Exception reason) {
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
40 if (m_cancel != null) {
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
41 try {
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
42 var p = m_cancel(reason);
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
43 p.On(SetResult, HandleErrorInternal, SetCancelledInternal);
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
44 CancellationRequested(p.Cancel);
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
45 } catch (Exception err) {
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
46 HandleErrorInternal(err);
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
47 }
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
48 } else {
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
49 HandleErrorInternal(reason ?? new OperationCanceledException());
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 149
diff changeset
50 }
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
51 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
52 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
53 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
54