annotate Implab/ActionChainTaskBase.cs @ 188:3071220371f8 ref20160224

Updated VS project
author cin
date Fri, 22 Apr 2016 12:59:31 +0300
parents dd4a3590f9c6
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 using System.Threading;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
3
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
4 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: 186
diff changeset
5 public class ActionChainTaskBase : AbstractTask {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
6 readonly Func<Exception, IPromise> m_error;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
7 readonly Func<Exception, IPromise> m_cancel;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
8
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
9 protected ActionChainTaskBase(Func<Exception, IPromise> error, Func<Exception, IPromise> cancel, bool autoCancellable) {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
10 m_error = error;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
11 m_cancel = cancel;
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
12 if (autoCancellable)
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
13 CancellationRequested(CancelOperation);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
14 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
15
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
16 public void Reject(Exception error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
17 if (LockCancelation())
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
18 HandleErrorInternal(error);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
19 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
20
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
21 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: 186
diff changeset
22 if (LockCancelation())
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
23 // отмена вызвана до начала выполнения задачи
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
24 HandleCancelInternal(reason);
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
25 }
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
26
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
27 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: 186
diff changeset
28 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: 186
diff changeset
29 try {
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
30 // вызываем обработчик отмены
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
31 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: 186
diff changeset
32 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: 186
diff changeset
33 // сообщаем асинхронной операции, что клиент уже не хочет получать результат
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
34 // т.е. если он инициировал отмену, задача отменилась, вызвался обрабочик отмены
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
35 // отбработчику сообщили, что результат уже не нужен и уже сам обработчик решает
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
36 // отдавать ли результат или подтвердить отмену (или вернуть ошибку).
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
37 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: 186
diff changeset
38 } 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: 186
diff changeset
39 HandleErrorInternal(err);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
40 }
187
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
41 } else {
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
42 HandleErrorInternal(reason ?? new OperationCanceledException());
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
43 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
44 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
45
187
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
46 protected void HandleErrorInternal(Exception error) {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
47 if (m_error != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
48 try {
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
49 var p = m_error(error);
187
dd4a3590f9c6 Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents: 186
diff changeset
50 p.On(SetResult, SetErrorInternal, SetCancelledInternal);
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
51 CancellationRequested(p.Cancel);
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
52 } 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: 186
diff changeset
53 SetErrorInternal(error);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
54 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
55 } else {
186
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
56 SetErrorInternal(error);
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
57 }
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
58 }
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
59
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
60 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
61 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
62