Mercurial > pub > ImplabNet
comparison Implab/AbstractTaskT.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 | |
children |
comparison
equal
deleted
inserted
replaced
186:75103928da09 | 187:dd4a3590f9c6 |
---|---|
1 using System; | |
2 using System.Threading; | |
3 | |
4 namespace Implab { | |
5 public abstract class AbstractTask<T> : AbstractPromise<T> { | |
6 int m_cancelationLock; | |
7 | |
8 /// <summary> | |
9 /// Получает эксклюзивное право отмены задания, используется для отмены задания до начала его выполнения. | |
10 /// </summary> | |
11 /// <returns><c>true</c>, if cancelation was locked, <c>false</c> otherwise.</returns> | |
12 protected bool LockCancelation() { | |
13 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0); | |
14 } | |
15 | |
16 | |
17 | |
18 protected void SetErrorInternal(Exception error) { | |
19 // unwrap | |
20 while (error is PromiseTransientException && error.InnerException != null) | |
21 error = error.InnerException; | |
22 | |
23 if (error is OperationCanceledException) | |
24 SetCancelled(error); | |
25 else | |
26 SetError(error); | |
27 } | |
28 | |
29 protected void SetCancelledInternal(Exception reason) { | |
30 SetCancelled( | |
31 reason == null ? new OperationCanceledException() : reason is OperationCanceledException ? reason : new OperationCanceledException(null, reason) | |
32 ); | |
33 } | |
34 } | |
35 } | |
36 |