public void RunTest() { string test = null; var runner = AsyncRunner <string> .Create(() => test = "HELLO WORLD!"); runner.IsExecuting.Subscribe(isExecuting => { }); runner.Load(); runner.GetInvokedTask().Wait(); Assert.That(test == "HELLO WORLD!"); }
protected EditableContext() { this._runner = AsyncRunner <Unit> .Create(() => this.ApplyAction(EditableObject)); this._runner.CompletedNotification.ObserveOnUiSafeScheduler() .Subscribe(_ => this.OnCompleteAction(this.EditableObject)); this._runner.IsExecuting.Subscribe( isExecuting => { lock (_locker) { this.IsBusy = isExecuting; } }); _runner.ThrownExceptions.ObserveOnUiSafeScheduler().Subscribe(this.OnError); }
public void Invoke <TMessage>(ConsumerDescriptor descriptor, IConsumer consumer, ConsumeContext <TMessage> envelope) where TMessage : class { var d = descriptor; var p = descriptor.WithEnvelope ? (object)envelope : envelope.Message; var fastInvoker = FastInvoker.GetInvoker(d.Method); if (!d.FireForget && !d.IsAsync) { // The all synch case try { InvokeCore(); } catch (Exception ex) { HandleException(ex, d); } } else if (!d.FireForget && d.IsAsync) { //// The awaitable Task case //BeginInvoke((Task)InvokeCore(cancelToken: AsyncRunner.AppShutdownCancellationToken), EndInvoke, d); // For now we must go with the AsyncRunner, the above call to BeginInvoke (APM > TPL) does not always // guarantee that the task is awaited and throws exceptions especially when EF is involved. using (var runner = AsyncRunner.Create()) { try { runner.Run((Task)InvokeCore(cancelToken: AsyncRunner.AppShutdownCancellationToken)); } catch (Exception ex) { HandleException(ex, d); } } } else if (d.FireForget && !d.IsAsync) { // A synch method should be executed async (without awaiting) AsyncRunner.Run((c, ct, state) => InvokeCore(c, ct), d) .ContinueWith(t => EndInvoke(t), /*TaskContinuationOptions.OnlyOnFaulted*/ TaskContinuationOptions.None) .ConfigureAwait(false); } else if (d.FireForget && d.IsAsync) { // An async (Task) method should be executed without awaiting AsyncRunner.Run((c, ct) => (Task)InvokeCore(c, ct), d) .ContinueWith(t => EndInvoke(t), TaskContinuationOptions.OnlyOnFaulted) .ConfigureAwait(false); } object InvokeCore(IComponentContext c = null, CancellationToken cancelToken = default(CancellationToken)) { if (d.Parameters.Length == 0) { // Only one method param: the message! return(fastInvoker.Invoke(consumer, p)); } var parameters = new object[d.Parameters.Length + 1]; parameters[0] = p; int i = 0; foreach (var obj in ResolveParameters(c, d, cancelToken).ToArray()) { i++; parameters[i] = obj; } return(fastInvoker.Invoke(consumer, parameters)); } }