Mercurial > pub > ImplabNet
annotate Implab/FuncTaskBase.cs @ 195:ea485487a424 v2
minor changes
author | cin |
---|---|
date | Wed, 04 May 2016 12:28:08 +0300 |
parents | dd4a3590f9c6 |
children | 40d7fed4a09e |
rev | line source |
---|---|
144 | 1 using System; |
2 | |
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 FuncTaskBase<TResult> : AbstractTask<TResult> { |
144 | 5 readonly Func<Exception, TResult> m_cancel; |
6 readonly Func<Exception, TResult> m_error; | |
7 | |
149 | 8 protected FuncTaskBase( Func<Exception, TResult> error, Func<Exception, TResult> cancel, bool autoCancellable) { |
144 | 9 m_error = error; |
10 m_cancel = cancel; | |
149 | 11 if (autoCancellable) |
12 CancellationRequested(CancelOperation); | |
144 | 13 } |
14 | |
15 public void Reject(Exception error) { | |
16 Safe.ArgumentNotNull(error, "error"); | |
17 if (LockCancelation()) | |
18 HandleErrorInternal(error); | |
19 } | |
20 | |
21 protected void HandleErrorInternal(Exception error) { | |
22 if (m_error != null) { | |
23 try { | |
24 SetResult(m_error(error)); | |
25 } 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
|
26 SetErrorInternal(err); |
144 | 27 } |
28 } 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
|
29 SetErrorInternal(error); |
144 | 30 } |
31 } | |
32 | |
33 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
|
34 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
|
35 HandleCancelInternal(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
|
36 } |
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
|
37 |
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
|
38 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
|
39 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
|
40 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
|
41 SetResult(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
|
42 } 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
|
43 HandleErrorInternal(err); |
144 | 44 } |
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
|
45 } 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
|
46 HandleErrorInternal(reason ?? new OperationCanceledException()); |
144 | 47 } |
48 } | |
49 | |
50 } | |
51 } | |
52 |