annotate Implab/Parallels/AsyncPool.cs @ 11:6ec82bf68c8e promises

refactoring
author cin
date Tue, 05 Nov 2013 01:09:58 +0400
parents
children e943453e5039
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
6ec82bf68c8e refactoring
cin
parents:
diff changeset
1 using System;
6ec82bf68c8e refactoring
cin
parents:
diff changeset
2 using System.Threading;
6ec82bf68c8e refactoring
cin
parents:
diff changeset
3
6ec82bf68c8e refactoring
cin
parents:
diff changeset
4 namespace Implab.Parallels {
6ec82bf68c8e refactoring
cin
parents:
diff changeset
5 /// <summary>
6ec82bf68c8e refactoring
cin
parents:
diff changeset
6 /// Класс для распаралеливания задач.
6ec82bf68c8e refactoring
cin
parents:
diff changeset
7 /// </summary>
6ec82bf68c8e refactoring
cin
parents:
diff changeset
8 /// <remarks>
6ec82bf68c8e refactoring
cin
parents:
diff changeset
9 /// Используя данный класс и лямда выражения можно распараллелить
6ec82bf68c8e refactoring
cin
parents:
diff changeset
10 /// вычисления, для этого используется концепция обещаний.
6ec82bf68c8e refactoring
cin
parents:
diff changeset
11 /// </remarks>
6ec82bf68c8e refactoring
cin
parents:
diff changeset
12 public static class AsyncPool {
6ec82bf68c8e refactoring
cin
parents:
diff changeset
13
6ec82bf68c8e refactoring
cin
parents:
diff changeset
14 public static Promise<T> Invoke<T>(Func<T> func) {
6ec82bf68c8e refactoring
cin
parents:
diff changeset
15 var p = new Promise<T>();
6ec82bf68c8e refactoring
cin
parents:
diff changeset
16
6ec82bf68c8e refactoring
cin
parents:
diff changeset
17 ThreadPool.QueueUserWorkItem(param => {
6ec82bf68c8e refactoring
cin
parents:
diff changeset
18 try {
6ec82bf68c8e refactoring
cin
parents:
diff changeset
19 p.Resolve(func());
6ec82bf68c8e refactoring
cin
parents:
diff changeset
20 } catch(Exception e) {
6ec82bf68c8e refactoring
cin
parents:
diff changeset
21 p.Reject(e);
6ec82bf68c8e refactoring
cin
parents:
diff changeset
22 }
6ec82bf68c8e refactoring
cin
parents:
diff changeset
23 });
6ec82bf68c8e refactoring
cin
parents:
diff changeset
24
6ec82bf68c8e refactoring
cin
parents:
diff changeset
25 return p;
6ec82bf68c8e refactoring
cin
parents:
diff changeset
26 }
6ec82bf68c8e refactoring
cin
parents:
diff changeset
27 }
6ec82bf68c8e refactoring
cin
parents:
diff changeset
28 }