public IAsync Enqueue(QueueSide qSide, Action callback) { Validate.Begin().IsNotNull <Action>(callback, "callback").IsTrue(((qSide == QueueSide.Front) || (qSide == QueueSide.Back)), "qSide is not a valid member of the QueueSide enumeration").Check(); IAsyncSource source = Async.NewSource(); if (this.pleaseAbort) { source.Throw(new OperationCanceledException("The work queue has shut down.")); } else { object sync = this.Sync; lock (sync) { Action item = delegate { callback.Try().Into(source); }; Action action2 = delegate { source.Throw(new OperationCanceledException("The dispatcher has shut down.")); }; if (qSide == QueueSide.Front) { this.runFnQ.EnqueueFront(item); this.cancelFnQ.EnqueueFront(action2); } else { this.runFnQ.Enqueue(item); this.cancelFnQ.Enqueue(action2); } ThreadPool.QueueUserWorkItem(this.threadPoolCallback); } } return(source.AsReceiveOnly()); }
public IAsync Receive(Action <Result> handler) { IAsyncSource source = Async.NewSource(); bool flag = false; object sync = this.sync; lock (sync) { if (this.result != null) { flag = true; } else { if (this.handlers == null) { this.handlers = new List <Tuple <Action <Result>, IAsyncSource> >(1); } this.handlers.Add(Tuple.Create <Action <Result>, IAsyncSource>(handler, source)); } } if (flag) { Result result = handler.Try <Result>(this.result); source.SetResult(result); } return(source.AsReceiveOnly()); }
public static IAsync <T> BeginEval <T>(this IAsyncWorkQueue queue, Func <T> f) { IAsyncSource <T> source = Async.NewSource <T>(); queue.BeginTry(delegate { try { Result <T> result = f.Eval <T>(); source.SetResult(result); } catch (Exception exception) { source.Throw <T>(new TargetInvocationException(exception)); } }); return(source.AsReceiveOnly <T>()); }
public IAsync BeginTry(Action callback) { IAsyncSource source = Async.NewSource(); this.dispatcher.BeginInvoke(delegate { try { Result @void = Result.Void; callback(); source.SetResult(@void); } catch (Exception exception1) { Result result = Result.NewError(exception1); source.SetResult(result); } }, Array.Empty <object>()); return(source.AsReceiveOnly()); }