AsyncPool.cs
155 lines
| 4.8 KiB
| text/x-csharp
|
CSharpLexer
cin
|
r35 | using Implab.Diagnostics; | ||
cin
|
r25 | using System; | ||
using System.Threading; | ||||
cin
|
r120 | using System.Linq; | ||
cin
|
r25 | |||
namespace Implab.Parallels { | ||||
/// <summary> | ||||
/// Класс для распаралеливания задач. | ||||
/// </summary> | ||||
/// <remarks> | ||||
/// Используя данный класс и лямда выражения можно распараллелить | ||||
/// вычисления, для этого используется концепция обещаний. | ||||
/// </remarks> | ||||
public static class AsyncPool { | ||||
cin
|
r45 | public static IPromise<T> Invoke<T>(Func<T> func) { | ||
cin
|
r25 | var p = new Promise<T>(); | ||
cin
|
r92 | var caller = TraceContext.Instance.CurrentOperation; | ||
cin
|
r25 | |||
ThreadPool.QueueUserWorkItem(param => { | ||||
cin
|
r92 | TraceContext.Instance.EnterLogicalOperation(caller,false); | ||
cin
|
r25 | try { | ||
cin
|
r14 | p.Resolve(func()); | ||
cin
|
r25 | } catch(Exception e) { | ||
p.Reject(e); | ||||
cin
|
r92 | } finally { | ||
TraceContext.Instance.Leave(); | ||||
} | ||||
cin
|
r25 | }); | ||
return p; | ||||
cin
|
r14 | } | ||
cin
|
r145 | public static IPromise<T> Invoke<T>(Func<ICancellationToken, T> func) { | ||
var p = new Promise<T>(); | ||||
var caller = TraceContext.Instance.CurrentOperation; | ||||
ThreadPool.QueueUserWorkItem(param => { | ||||
TraceContext.Instance.EnterLogicalOperation(caller,false); | ||||
try { | ||||
p.Resolve(func(p)); | ||||
} catch(Exception e) { | ||||
p.Reject(e); | ||||
} finally { | ||||
TraceContext.Instance.Leave(); | ||||
} | ||||
}); | ||||
return p; | ||||
} | ||||
cin
|
r124 | public static IPromise<T> RunThread<T>(Func<T> func) { | ||
cin
|
r14 | var p = new Promise<T>(); | ||
cin
|
r92 | var caller = TraceContext.Instance.CurrentOperation; | ||
cin
|
r35 | |||
cin
|
r14 | var worker = new Thread(() => { | ||
cin
|
r92 | TraceContext.Instance.EnterLogicalOperation(caller,false); | ||
cin
|
r14 | try { | ||
p.Resolve(func()); | ||||
} catch (Exception e) { | ||||
p.Reject(e); | ||||
cin
|
r92 | } finally { | ||
TraceContext.Instance.Leave(); | ||||
cin
|
r14 | } | ||
}); | ||||
worker.IsBackground = true; | ||||
worker.Start(); | ||||
return p; | ||||
cin
|
r25 | } | ||
cin
|
r48 | |||
cin
|
r145 | public static IPromise<T> RunThread<T>(Func<ICancellationToken, T> func) { | ||
var p = new Promise<T>(); | ||||
var caller = TraceContext.Instance.CurrentOperation; | ||||
var worker = new Thread(() => { | ||||
TraceContext.Instance.EnterLogicalOperation(caller,false); | ||||
try { | ||||
p.Resolve(func(p)); | ||||
} catch (Exception e) { | ||||
p.Reject(e); | ||||
} finally { | ||||
TraceContext.Instance.Leave(); | ||||
} | ||||
}); | ||||
worker.IsBackground = true; | ||||
worker.Start(); | ||||
return p; | ||||
} | ||||
cin
|
r48 | |||
cin
|
r124 | public static IPromise RunThread(Action func) { | ||
cin
|
r119 | var p = new Promise(); | ||
cin
|
r48 | |||
cin
|
r92 | var caller = TraceContext.Instance.CurrentOperation; | ||
cin
|
r48 | |||
var worker = new Thread(() => { | ||||
cin
|
r92 | TraceContext.Instance.EnterLogicalOperation(caller,false); | ||
cin
|
r48 | try { | ||
func(); | ||||
p.Resolve(); | ||||
} catch (Exception e) { | ||||
p.Reject(e); | ||||
cin
|
r92 | } finally { | ||
TraceContext.Instance.Leave(); | ||||
cin
|
r48 | } | ||
}); | ||||
worker.IsBackground = true; | ||||
worker.Start(); | ||||
return p; | ||||
} | ||||
cin
|
r120 | |||
cin
|
r145 | public static IPromise RunThread(Action<ICancellationToken> func) { | ||
var p = new Promise(); | ||||
var caller = TraceContext.Instance.CurrentOperation; | ||||
var worker = new Thread(() => { | ||||
TraceContext.Instance.EnterLogicalOperation(caller,false); | ||||
try { | ||||
func(p); | ||||
p.Resolve(); | ||||
} catch (Exception e) { | ||||
p.Reject(e); | ||||
} finally { | ||||
TraceContext.Instance.Leave(); | ||||
} | ||||
}); | ||||
worker.IsBackground = true; | ||||
worker.Start(); | ||||
return p; | ||||
} | ||||
cin
|
r121 | public static IPromise[] RunThread(params Action[] func) { | ||
cin
|
r124 | return func.Select(f => RunThread(f)).ToArray(); | ||
cin
|
r120 | } | ||
cin
|
r145 | public static IPromise[] RunThread(params Action<ICancellationToken>[] func) { | ||
return func.Select(f => RunThread(f)).ToArray(); | ||||
} | ||||
cin
|
r121 | public static IPromise<T>[] RunThread<T>(params Func<T>[] func) { | ||
cin
|
r124 | return func.Select(f => RunThread(f)).ToArray(); | ||
cin
|
r120 | } | ||
cin
|
r145 | |||
public static IPromise<T>[] RunThread<T>(params Func<ICancellationToken, T>[] func) { | ||||
return func.Select(f => RunThread(f)).ToArray(); | ||||
} | ||||
cin
|
r25 | } | ||
} | ||||