private async Task ConnectAsync(CancellationToken ct) { var provider = _services.GetService <IRInteractiveWorkflowProvider>(); _workflow = provider.GetOrCreate(); _ui = _services.GetService <IHostUIService>(); _ui.SetLogLevel(MessageType.Info); var e = GetREngine(); if (e == null) { return; } var log = _services.Log(); var info = BrokerConnectionInfo.Create("(local)", e.InstallPath, e.Architecture, string.Empty); var start = DateTime.Now; _ui.LogMessageAsync($"Starting R Process with {e.InstallPath}...", MessageType.Info).DoNotWait(); try { if (await _workflow.RSessions.TrySwitchBrokerAsync("(local)", info, ct)) { try { await _workflow.RSession.StartHostAsync(new RHostStartupInfo(), new RSessionCallback(), _services.UI(), Debugger.IsAttached? 1000000 : 20000, ct); } catch (Exception ex) { _ui.ShowMessageAsync($"Unable to start Microsoft.R.Host process. Exception: {ex.Message}", MessageType.Error).DoNotWait(); return; } // Start package building _ui.LogMessageAsync($"complete in {FormatElapsed(DateTime.Now - start)}", MessageType.Info).DoNotWait(); start = DateTime.Now; _ui.LogMessageAsync("Building IntelliSense index...", MessageType.Info).DoNotWait(); _packageIndex = _services.GetService <IPackageIndex>(); _packageIndex.BuildIndexAsync(ct).ContinueWith(t => { _ui.LogMessageAsync($"complete in {FormatElapsed(DateTime.Now - start)}", MessageType.Info).DoNotWait(); }, ct, TaskContinuationOptions.None, TaskScheduler.Default).DoNotWait(); } else { _ui.ShowMessageAsync($"Unable to connect to broker.", MessageType.Error).DoNotWait(); } } catch (Exception ex) { _ui.ShowMessageAsync($"Unable to connect to broker. Exception: {ex.Message}", MessageType.Error).DoNotWait(); } }
public Task <T> SendAsync <T>(Func <T> action, ThreadPostPriority priority, IHostUIService ui, CancellationToken ct = default) { var tcs = new TaskCompletionSource <T>(); Execute(() => { try { tcs.TrySetResult(action()); } catch (OperationCanceledException) { tcs.TrySetCanceled(); } catch (Exception ex) { ui.LogMessageAsync($"Exception {ex.Message} at {ex.StackTrace}", MessageType.Error).DoNotWait(); tcs.TrySetException(ex); } }, priority); return(tcs.Task); }
public Task <T> SendAsync <T>(Func <Task <T> > action, ThreadPostPriority priority, IHostUIService ui, CancellationToken ct = default) { var tcs = new TaskCompletionSource <T>(); #pragma warning disable VSTHRD101 // Avoid unsupported async delegates Execute(async() => { try { tcs.TrySetResult(await action()); } catch (OperationCanceledException) { tcs.TrySetCanceled(); } catch (Exception ex) { ui.LogMessageAsync($"Exception {ex.Message} at {ex.StackTrace}", MessageType.Error).DoNotWait(); tcs.TrySetException(ex); } }, priority); #pragma warning restore VSTHRD101 // Avoid unsupported async delegates return(tcs.Task); }
/// <summary> /// Executed cancellable action on UI thread and return result. /// </summary> /// <param name="mainThread"></param> /// <param name="action"></param> /// <param name="cancellationToken"></param> public static async Task <T> SendAsync <T>(this IMainThread mainThread, Func <T> action, IHostUIService ui, CancellationToken cancellationToken = default) { try { await mainThread.SwitchToAsync(cancellationToken); return(action()); } catch (OperationCanceledException) { return(default);
/// <summary> /// Executed cancellable action on UI thread. /// </summary> /// <param name="mainThread"></param> /// <param name="action"></param> /// <param name="cancellationToken"></param> public static async Task SendAsync(this IMainThread mainThread, Action action, IHostUIService ui, CancellationToken cancellationToken = default) { await mainThread.SwitchToAsync(cancellationToken); try { action(); } catch (OperationCanceledException) { throw; } catch (Exception ex) { ui.LogMessageAsync($"Exception {ex.Message} at {ex.StackTrace}", MessageType.Error).DoNotWait(); throw; } }