comparison Implab/Parallels/AsyncPool.cs @ 145:706fccb85524 v2

RC: cancellation support for promises + tests
author cin
date Sun, 08 Mar 2015 02:52:27 +0300
parents a336cb13c6a9
children
comparison
equal deleted inserted replaced
144:8c0b95069066 145:706fccb85524
29 }); 29 });
30 30
31 return p; 31 return p;
32 } 32 }
33 33
34 public static IPromise<T> Invoke<T>(Func<ICancellationToken, T> func) {
35 var p = new Promise<T>();
36 var caller = TraceContext.Instance.CurrentOperation;
37
38 ThreadPool.QueueUserWorkItem(param => {
39 TraceContext.Instance.EnterLogicalOperation(caller,false);
40 try {
41 p.Resolve(func(p));
42 } catch(Exception e) {
43 p.Reject(e);
44 } finally {
45 TraceContext.Instance.Leave();
46 }
47 });
48
49 return p;
50 }
51
34 public static IPromise<T> RunThread<T>(Func<T> func) { 52 public static IPromise<T> RunThread<T>(Func<T> func) {
35 var p = new Promise<T>(); 53 var p = new Promise<T>();
36 54
37 var caller = TraceContext.Instance.CurrentOperation; 55 var caller = TraceContext.Instance.CurrentOperation;
38 56
39 var worker = new Thread(() => { 57 var worker = new Thread(() => {
40 TraceContext.Instance.EnterLogicalOperation(caller,false); 58 TraceContext.Instance.EnterLogicalOperation(caller,false);
41 try { 59 try {
42 p.Resolve(func()); 60 p.Resolve(func());
61 } catch (Exception e) {
62 p.Reject(e);
63 } finally {
64 TraceContext.Instance.Leave();
65 }
66 });
67 worker.IsBackground = true;
68 worker.Start();
69
70 return p;
71 }
72
73 public static IPromise<T> RunThread<T>(Func<ICancellationToken, T> func) {
74 var p = new Promise<T>();
75
76 var caller = TraceContext.Instance.CurrentOperation;
77
78 var worker = new Thread(() => {
79 TraceContext.Instance.EnterLogicalOperation(caller,false);
80 try {
81 p.Resolve(func(p));
43 } catch (Exception e) { 82 } catch (Exception e) {
44 p.Reject(e); 83 p.Reject(e);
45 } finally { 84 } finally {
46 TraceContext.Instance.Leave(); 85 TraceContext.Instance.Leave();
47 } 86 }
73 worker.Start(); 112 worker.Start();
74 113
75 return p; 114 return p;
76 } 115 }
77 116
117 public static IPromise RunThread(Action<ICancellationToken> func) {
118 var p = new Promise();
119
120 var caller = TraceContext.Instance.CurrentOperation;
121
122 var worker = new Thread(() => {
123 TraceContext.Instance.EnterLogicalOperation(caller,false);
124 try {
125 func(p);
126 p.Resolve();
127 } catch (Exception e) {
128 p.Reject(e);
129 } finally {
130 TraceContext.Instance.Leave();
131 }
132 });
133 worker.IsBackground = true;
134 worker.Start();
135
136 return p;
137 }
138
78 public static IPromise[] RunThread(params Action[] func) { 139 public static IPromise[] RunThread(params Action[] func) {
140 return func.Select(f => RunThread(f)).ToArray();
141 }
142
143 public static IPromise[] RunThread(params Action<ICancellationToken>[] func) {
79 return func.Select(f => RunThread(f)).ToArray(); 144 return func.Select(f => RunThread(f)).ToArray();
80 } 145 }
81 146
82 public static IPromise<T>[] RunThread<T>(params Func<T>[] func) { 147 public static IPromise<T>[] RunThread<T>(params Func<T>[] func) {
83 return func.Select(f => RunThread(f)).ToArray(); 148 return func.Select(f => RunThread(f)).ToArray();
84 } 149 }
150
151 public static IPromise<T>[] RunThread<T>(params Func<ICancellationToken, T>[] func) {
152 return func.Select(f => RunThread(f)).ToArray();
153 }
85 } 154 }
86 } 155 }