public async Task DeviceDestroyedAsync(Guid deviceId, CancellationToken cancellationToken) { await _mainThread.SwitchToAsync(cancellationToken); var device = FindDevice(deviceId); Debug.Assert(device != null, "List of devices is out of sync."); if (device == null) { return; } lock (_devicesLock) { _devices.Remove(device); } IRPlotDeviceVisualComponent component; if (_assignedVisualComponents.TryGetValue(deviceId, out component)) { component.Unassign(); _assignedVisualComponents.Remove(deviceId); _unassignedVisualComponents.Add(component); } else { Debug.Assert(false, "Failed to destroy a plot visual component."); } DeviceRemoved?.Invoke(this, new RPlotDeviceEventArgs(device)); }
/// <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; } }
private async Task WriteAsync(string text, bool isError) { await _mainThread.SwitchToAsync(_disposeToken.CancellationToken); if (_component == null) { _component = await _workflow.GetOrCreateVisualComponentAsync(); _component.Container.Show(focus: false, immediate: false); } if (isError) { _component.InteractiveWindow.WriteError(text); } else { _component.InteractiveWindow.Write(text); } }
public async Task RefreshEnvironmentsAsync(CancellationToken cancellationToken = default(CancellationToken)) { using (_disposeToken.Link(ref cancellationToken)) { var session = _rSession; await TaskUtilities.SwitchToBackgroundThread(); var envs = new List <REnvironment>(); bool success = false; try { var traceback = (await session.TracebackAsync(cancellationToken: cancellationToken)) .Skip(1) // skip global - it's in the search path already .Select(frame => new REnvironment(frame)) .Reverse(); if (traceback.Any()) { envs.AddRange(traceback); envs.Add(null); } var searchPath = (await session.EvaluateAsync <string[]>("as.list(search())", REvaluationKind.BaseEnv, cancellationToken)) .Except(_hiddenEnvironments) .Select(name => new REnvironment(name)); envs.AddRange(searchPath); success = true; } catch (RException) { } catch (OperationCanceledException) { } await _mainThread.SwitchToAsync(cancellationToken); var oldSelection = _selectedEnvironment; _environments.ReplaceWith(success ? envs : _errorEnvironments); IREnvironment newSelection = null; if (oldSelection != null) { newSelection = _environments?.FirstOrDefault(env => env?.Name == oldSelection.Name); } SelectedEnvironment = newSelection ?? _environments.FirstOrDefault(); } }
public async Task <IDisposable> LockCredentialsAsync(CancellationToken cancellationToken = default(CancellationToken)) { // If there is already a LockCredentialsAsync request for which there hasn't been a validation yet, wait until it completes. // This can happen when two sessions are being created concurrently, and we don't want to pop the credential prompt twice - // the first prompt should be validated and saved, and then the same credentials will be reused for the second session. var token = await _lock.WriterLockAsync(cancellationToken); await _mainThread.SwitchToAsync(cancellationToken); try { var credentials = _credentials ?? _securityService.GetUserCredentials(_authority, _workspaceName); _credentials = credentials; } catch (Exception) { token.Dispose(); throw; } return(Disposable.Create(() => { token.Dispose(); })); }
/// <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, CancellationToken cancellationToken = default) { await mainThread.SwitchToAsync(cancellationToken); return(action()); }
/// <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, CancellationToken cancellationToken = default) { await mainThread.SwitchToAsync(cancellationToken); action(); }