Mercurial > pub > ImplabNet
annotate Implab/Parallels/AsyncPool.cs @ 243:b1e0ffdf3451 v3
working on promises
author | cin |
---|---|
date | Wed, 24 Jan 2018 19:24:10 +0300 |
parents | 706fccb85524 |
children |
rev | line source |
---|---|
35 | 1 using Implab.Diagnostics; |
25 | 2 using System; |
3 using System.Threading; | |
120 | 4 using System.Linq; |
25 | 5 |
6 namespace Implab.Parallels { | |
7 /// <summary> | |
8 /// Класс для распаралеливания задач. | |
9 /// </summary> | |
10 /// <remarks> | |
11 /// Используя данный класс и лямда выражения можно распараллелить | |
12 /// вычисления, для этого используется концепция обещаний. | |
13 /// </remarks> | |
14 public static class AsyncPool { | |
15 | |
45 | 16 public static IPromise<T> Invoke<T>(Func<T> func) { |
25 | 17 var p = new Promise<T>(); |
92 | 18 var caller = TraceContext.Instance.CurrentOperation; |
25 | 19 |
20 ThreadPool.QueueUserWorkItem(param => { | |
92 | 21 TraceContext.Instance.EnterLogicalOperation(caller,false); |
25 | 22 try { |
14 | 23 p.Resolve(func()); |
25 | 24 } catch(Exception e) { |
25 p.Reject(e); | |
92 | 26 } finally { |
27 TraceContext.Instance.Leave(); | |
28 } | |
25 | 29 }); |
30 | |
31 return p; | |
14 | 32 } |
33 | |
145 | 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 | |
124 | 52 public static IPromise<T> RunThread<T>(Func<T> func) { |
14 | 53 var p = new Promise<T>(); |
54 | |
92 | 55 var caller = TraceContext.Instance.CurrentOperation; |
35 | 56 |
14 | 57 var worker = new Thread(() => { |
92 | 58 TraceContext.Instance.EnterLogicalOperation(caller,false); |
14 | 59 try { |
60 p.Resolve(func()); | |
61 } catch (Exception e) { | |
62 p.Reject(e); | |
92 | 63 } finally { |
64 TraceContext.Instance.Leave(); | |
14 | 65 } |
66 }); | |
67 worker.IsBackground = true; | |
68 worker.Start(); | |
69 | |
70 return p; | |
25 | 71 } |
48 | 72 |
145 | 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)); | |
82 } catch (Exception e) { | |
83 p.Reject(e); | |
84 } finally { | |
85 TraceContext.Instance.Leave(); | |
86 } | |
87 }); | |
88 worker.IsBackground = true; | |
89 worker.Start(); | |
90 | |
91 return p; | |
92 } | |
93 | |
48 | 94 |
124 | 95 public static IPromise RunThread(Action func) { |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
92
diff
changeset
|
96 var p = new Promise(); |
48 | 97 |
92 | 98 var caller = TraceContext.Instance.CurrentOperation; |
48 | 99 |
100 var worker = new Thread(() => { | |
92 | 101 TraceContext.Instance.EnterLogicalOperation(caller,false); |
48 | 102 try { |
103 func(); | |
104 p.Resolve(); | |
105 } catch (Exception e) { | |
106 p.Reject(e); | |
92 | 107 } finally { |
108 TraceContext.Instance.Leave(); | |
48 | 109 } |
110 }); | |
111 worker.IsBackground = true; | |
112 worker.Start(); | |
113 | |
114 return p; | |
115 } | |
120 | 116 |
145 | 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 | |
121 | 139 public static IPromise[] RunThread(params Action[] func) { |
124 | 140 return func.Select(f => RunThread(f)).ToArray(); |
120 | 141 } |
142 | |
145 | 143 public static IPromise[] RunThread(params Action<ICancellationToken>[] func) { |
144 return func.Select(f => RunThread(f)).ToArray(); | |
145 } | |
146 | |
121 | 147 public static IPromise<T>[] RunThread<T>(params Func<T>[] func) { |
124 | 148 return func.Select(f => RunThread(f)).ToArray(); |
120 | 149 } |
145 | 150 |
151 public static IPromise<T>[] RunThread<T>(params Func<ICancellationToken, T>[] func) { | |
152 return func.Select(f => RunThread(f)).ToArray(); | |
153 } | |
25 | 154 } |
155 } |