annotate Implab/AsyncPool.cs @ 1:6fb3b01ee971

Added utility class for safe disposing methods. Added event fireing while promise is cancelled (needs some more work)
author cin
date Tue, 27 Aug 2013 08:28:42 +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 }