Mercurial > pub > ImplabNet
annotate Implab/Parallels/AsyncPool.cs @ 134:04d4c92d0f28 v2
Improved logging
author | cin |
---|---|
date | Wed, 11 Feb 2015 02:12:15 +0300 |
parents | a336cb13c6a9 |
children | 706fccb85524 |
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 | |
124 | 34 public static IPromise<T> RunThread<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 | |
124 | 56 public static IPromise RunThread(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 |
121 | 78 public static IPromise[] RunThread(params Action[] func) { |
124 | 79 return func.Select(f => RunThread(f)).ToArray(); |
120 | 80 } |
81 | |
121 | 82 public static IPromise<T>[] RunThread<T>(params Func<T>[] func) { |
124 | 83 return func.Select(f => RunThread(f)).ToArray(); |
120 | 84 } |
25 | 85 } |
86 } |