comparison Implab/Parallels/AsyncPool.cs @ 92:4c0e5ef99986 v2

rewritten tracing
author cin
date Wed, 22 Oct 2014 18:37:56 +0400
parents 790e8a997d30
children 2573b562e328
comparison
equal deleted inserted replaced
91:cdaaf4792c22 92:4c0e5ef99986
12 /// </remarks> 12 /// </remarks>
13 public static class AsyncPool { 13 public static class AsyncPool {
14 14
15 public static IPromise<T> Invoke<T>(Func<T> func) { 15 public static IPromise<T> Invoke<T>(Func<T> func) {
16 var p = new Promise<T>(); 16 var p = new Promise<T>();
17 var caller = TraceContext.Snapshot(); 17 var caller = TraceContext.Instance.CurrentOperation;
18 18
19 ThreadPool.QueueUserWorkItem(param => { 19 ThreadPool.QueueUserWorkItem(param => {
20 TraceContext.Fork(caller); 20 TraceContext.Instance.EnterLogicalOperation(caller,false);
21 try { 21 try {
22 p.Resolve(func()); 22 p.Resolve(func());
23 } catch(Exception e) { 23 } catch(Exception e) {
24 p.Reject(e); 24 p.Reject(e);
25 } 25 } finally {
26 TraceContext.Instance.Leave();
27 }
26 }); 28 });
27 29
28 return p; 30 return p;
29 } 31 }
30 32
31 public static IPromise<T> InvokeNewThread<T>(Func<T> func) { 33 public static IPromise<T> InvokeNewThread<T>(Func<T> func) {
32 var p = new Promise<T>(); 34 var p = new Promise<T>();
33 35
34 var caller = TraceContext.Snapshot(); 36 var caller = TraceContext.Instance.CurrentOperation;
35 37
36 var worker = new Thread(() => { 38 var worker = new Thread(() => {
37 TraceContext.Fork(caller); 39 TraceContext.Instance.EnterLogicalOperation(caller,false);
38 try { 40 try {
39 p.Resolve(func()); 41 p.Resolve(func());
40 } catch (Exception e) { 42 } catch (Exception e) {
41 p.Reject(e); 43 p.Reject(e);
44 } finally {
45 TraceContext.Instance.Leave();
42 } 46 }
43 }); 47 });
44 worker.IsBackground = true; 48 worker.IsBackground = true;
45 worker.Start(); 49 worker.Start();
46 50
49 53
50 54
51 public static IPromise InvokeNewThread(Action func) { 55 public static IPromise InvokeNewThread(Action func) {
52 var p = new Promise<object>(); 56 var p = new Promise<object>();
53 57
54 var caller = TraceContext.Snapshot(); 58 var caller = TraceContext.Instance.CurrentOperation;
55 59
56 var worker = new Thread(() => { 60 var worker = new Thread(() => {
57 TraceContext.Fork(caller); 61 TraceContext.Instance.EnterLogicalOperation(caller,false);
58 try { 62 try {
59 func(); 63 func();
60 p.Resolve(); 64 p.Resolve();
61 } catch (Exception e) { 65 } catch (Exception e) {
62 p.Reject(e); 66 p.Reject(e);
67 } finally {
68 TraceContext.Instance.Leave();
63 } 69 }
64 }); 70 });
65 worker.IsBackground = true; 71 worker.IsBackground = true;
66 worker.Start(); 72 worker.Start();
67 73