comparison Implab/FuncTaskBase.cs @ 192:f1da3afc3521 release v2.1

Слияние с v2
author cin
date Fri, 22 Apr 2016 13:10:34 +0300
parents dd4a3590f9c6
children 40d7fed4a09e
comparison
equal deleted inserted replaced
71:1714fd8678ef 192:f1da3afc3521
1 using System;
2
3 namespace Implab {
4 public class FuncTaskBase<TResult> : AbstractTask<TResult> {
5 readonly Func<Exception, TResult> m_cancel;
6 readonly Func<Exception, TResult> m_error;
7
8 protected FuncTaskBase( Func<Exception, TResult> error, Func<Exception, TResult> cancel, bool autoCancellable) {
9 m_error = error;
10 m_cancel = cancel;
11 if (autoCancellable)
12 CancellationRequested(CancelOperation);
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) {
26 SetErrorInternal(err);
27 }
28 } else {
29 SetErrorInternal(error);
30 }
31 }
32
33 public override void CancelOperation(Exception reason) {
34 if (LockCancelation())
35 HandleCancelInternal(reason);
36 }
37
38 protected void HandleCancelInternal(Exception reason) {
39 if (m_cancel != null) {
40 try {
41 SetResult(m_cancel(reason));
42 } catch (Exception err) {
43 HandleErrorInternal(err);
44 }
45 } else {
46 HandleErrorInternal(reason ?? new OperationCanceledException());
47 }
48 }
49
50 }
51 }
52