annotate Implab/AsyncPool.cs @ 7:7ea9363fef6c promises

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