Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal class ThrottlePool
- {
- #region Worker
- internal class Worker
- {
- public object Request;
- public Delegate Callback;
- public Delegate Action;
- }
- #endregion
- private int concurrency;
- private int running;
- private System.Collections.Generic.Queue<Worker> workers;
- public ThrottlePool(int concurrency)
- {
- this.concurrency = concurrency;
- this.running = 0;
- this.workers = new Queue<Worker>();
- }
- public void Run<TRequest, TResponse>(Reactor.Action<TRequest, Reactor.Action<System.Exception, TResponse>> action, TRequest request, Reactor.Action<Exception, TResponse> callback)
- {
- this.workers.Enqueue(new Worker {
- Request = request,
- Callback = callback,
- Action = action,
- });
- this.Process();
- }
- private void Process()
- {
- if (this.workers.Count > 0) {
- if (this.running < this.concurrency) {
- var worker = this.workers.Dequeue();
- this.running++;
- var paramters = worker.Callback.Method.GetParameters();
- var callback = Reactor.DynamicAction.Create(new System.Type[] { paramters[0].ParameterType,
- paramters[1].ParameterType }, (results) => {
- this.running--;
- worker.Callback.DynamicInvoke(results);
- this.Process();
- });
- worker.Action.DynamicInvoke(new object[] { worker.Request, callback });
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement