private void RebuildIndex() { if (!_buildIndexLock.IsCompleted) { // Stull building, try again later ScheduleIdleTimeRebuild(); return; } _buildIndexLock.Reset(); _packages.Clear(); BuildIndexAsync().DoNotWait(); }
public async Task Reset() { var bal = new BinaryAsyncLock(); var count = 0; await ParallelTools.InvokeAsync(4, async i => { try { var isSet = await bal.WaitAsync(); if (!isSet) { await Task.Delay(50); bal.Reset(); } } catch (OperationCanceledException) { Interlocked.Increment(ref count); } }); count.Should().Be(3); }
public void ReloadItems() { _coreShell.AssertIsOnMainThread(); switch (_selectedTab) { case SelectedTab.AvailablePackages: _availableAndInstalledLock.Reset(); //LoadAvailablePackages(); break; case SelectedTab.InstalledPackages: //LoadInstalledPackages(); break; case SelectedTab.LoadedPackages: break; default: throw new ArgumentOutOfRangeException(); } }
private async Task ConnectToBrokerWorker() { Trace.Assert(_brokerProcess == null); string rhostBrokerExe = Path.Combine(_rhostDirectory, RHostBrokerExe); if (!File.Exists(rhostBrokerExe)) { throw new RHostBinaryMissingException(); } Process process = null; try { string pipeName = Guid.NewGuid().ToString(); using (var serverUriPipe = new NamedPipeServerStream(pipeName, PipeDirection.In)) { var psi = new ProcessStartInfo { FileName = rhostBrokerExe, UseShellExecute = false, Arguments = $" --logging:logHostOutput true" + $" --logging:logPackets true" + $" --server.urls http://127.0.0.1:0" + // :0 means first available ephemeral port $" --startup:name \"{_name}\"" + $" --startup:writeServerUrlsToPipe {pipeName}" + $" --lifetime:parentProcessId {Process.GetCurrentProcess().Id}" + $" --security:secret \"{_credentials.Password}\"" + $" --R:autoDetect false" + $" --R:interpreters:{InterpreterId}:basePath \"{_rHome}\"" }; if (!ShowConsole) { psi.CreateNoWindow = true; } process = Process.Start(psi); process.EnableRaisingEvents = true; var cts = new CancellationTokenSource(10000); process.Exited += delegate { cts.Cancel(); _brokerProcess = null; _connectLock.Reset(); }; await serverUriPipe.WaitForConnectionAsync(cts.Token); var serverUriData = new MemoryStream(); try { // Pipes are special in that a zero-length read is not an indicator of end-of-stream. // Stream.CopyTo uses a zero-length read as indicator of end-of-stream, so it cannot // be used here. Instead, copy the data manually, using PipeStream.IsConnected to detect // when the other side has finished writing and closed the pipe. var buffer = new byte[0x1000]; do { int count = await serverUriPipe.ReadAsync(buffer, 0, buffer.Length, cts.Token); serverUriData.Write(buffer, 0, count); } while (serverUriPipe.IsConnected); } catch (OperationCanceledException) { throw new RHostTimeoutException("Timed out while waiting for broker process to report its endpoint URI"); } string serverUriStr = Encoding.UTF8.GetString(serverUriData.ToArray()); Uri[] serverUri; try { serverUri = JsonConvert.DeserializeObject <Uri[]>(serverUriStr); } catch (JsonSerializationException ex) { throw new RHostDisconnectedException($"Invalid JSON for endpoint URIs received from broker ({ex.Message}): {serverUriStr}"); } if (serverUri?.Length != 1) { throw new RHostDisconnectedException($"Unexpected number of endpoint URIs received from broker: {serverUriStr}"); } CreateHttpClient(serverUri[0], _credentials); } _brokerProcess = process; DisposableBag.Add(DisposeBrokerProcess); } finally { if (_brokerProcess == null) { try { process?.Kill(); } catch (Exception) { } finally { process?.Dispose(); } } } }