annotate Implab/Parallels/AsyncPool.cs @ 43:7c2369f580b8

improved tracing, TextListenerBase can be bound to logical operation scope.
author cin
date Wed, 16 Apr 2014 10:12:56 +0400
parents fe33f4e02ad5
children d10034588e38
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
2880242f987a initial log capabilities
cin
parents: 25
diff changeset
1 using Implab.Diagnostics;
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
2 using System;
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
3 using System.Threading;
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
4
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
5 namespace Implab.Parallels {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
6 /// <summary>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
7 /// Класс для распаралеливания задач.
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
8 /// </summary>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
9 /// <remarks>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
10 /// Используя данный класс и лямда выражения можно распараллелить
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
11 /// вычисления, для этого используется концепция обещаний.
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
12 /// </remarks>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
13 public static class AsyncPool {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
14
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
15 public static Promise<T> Invoke<T>(Func<T> func) {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
16 var p = new Promise<T>();
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
17 var caller = TraceContext.Snapshot();
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
18
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
19 ThreadPool.QueueUserWorkItem(param => {
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
20 TraceContext.Transfer(caller);
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
21 try {
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
22 p.Resolve(func());
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
23 } catch(Exception e) {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
24 p.Reject(e);
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
25 }
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
26 });
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
27
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
28 return p;
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
29 }
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
30
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
31 public static Promise<T> InvokeNewThread<T>(Func<T> func) {
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
32 var p = new Promise<T>();
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
33
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
34 var caller = TraceContext.Snapshot();
35
2880242f987a initial log capabilities
cin
parents: 25
diff changeset
35
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
36 var worker = new Thread(() => {
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
37 TraceContext.Transfer(caller);
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
38 try {
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
39 p.Resolve(func());
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
40 } catch (Exception e) {
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
41 p.Reject(e);
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
42 }
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
43 });
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
44 worker.IsBackground = true;
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
45 worker.Start();
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
46
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
47 return p;
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
48 }
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
49 }
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
50 }