Mercurial > pub > ImplabNet
annotate Implab/Parallels/AsyncPool.cs @ 120:f1b897999260 v2
improved asyncpool usability
working on batch operations on asyncqueue
author | cin |
---|---|
date | Mon, 12 Jan 2015 05:19:52 +0300 |
parents | 2573b562e328 |
children | 62d2f1e98c4e |
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 | |
45 | 34 public static IPromise<T> InvokeNewThread<T>(Func<T> func) { |
14 | 35 var p = new Promise<T>(); |
36 | |
92 | 37 var caller = TraceContext.Instance.CurrentOperation; |
35 | 38 |
14 | 39 var worker = new Thread(() => { |
92 | 40 TraceContext.Instance.EnterLogicalOperation(caller,false); |
14 | 41 try { |
42 p.Resolve(func()); | |
43 } catch (Exception e) { | |
44 p.Reject(e); | |
92 | 45 } finally { |
46 TraceContext.Instance.Leave(); | |
14 | 47 } |
48 }); | |
49 worker.IsBackground = true; | |
50 worker.Start(); | |
51 | |
52 return p; | |
25 | 53 } |
48 | 54 |
55 | |
66 | 56 public static IPromise InvokeNewThread(Action func) { |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
92
diff
changeset
|
57 var p = new Promise(); |
48 | 58 |
92 | 59 var caller = TraceContext.Instance.CurrentOperation; |
48 | 60 |
61 var worker = new Thread(() => { | |
92 | 62 TraceContext.Instance.EnterLogicalOperation(caller,false); |
48 | 63 try { |
64 func(); | |
65 p.Resolve(); | |
66 } catch (Exception e) { | |
67 p.Reject(e); | |
92 | 68 } finally { |
69 TraceContext.Instance.Leave(); | |
48 | 70 } |
71 }); | |
72 worker.IsBackground = true; | |
73 worker.Start(); | |
74 | |
75 return p; | |
76 } | |
120 | 77 |
78 public static IPromise[] ThreadRun(params Action[] func) { | |
79 return func.Select(f => InvokeNewThread(f)).ToArray(); | |
80 } | |
81 | |
82 public static IPromise<T>[] ThreadRun<T>(params Func<T>[] func) { | |
83 return func.Select(f => InvokeNewThread(f)).ToArray(); | |
84 } | |
25 | 85 } |
86 } |