/// <summary> /// Stops th probe /// </summary> /// <param name="cancellationToken">A token to monitor for abort requests</param> /// <returns>True if stopped successfully</returns> public async Task <bool> StopAsync(CancellationToken cancellationToken) { var success = false; if (!Task.IsCompleted) { using (await _control.LockAsync(cancellationToken).ConfigureAwait(false)) { if (!Task.IsCompleted) { _continueProcessing = false; try { if (_processingTask != null) { await _processingTask.ConfigureAwait(false); _processingTask = null; } } catch (Exception) { // Suppress exceptions while allowing the processing task to complete } } try { if (_endpoint != null) { await _endpoint.StopAsync(cancellationToken).ConfigureAwait(false); } } catch (Exception) { // Suppress exceptions with stopping the endpoint } _endpoint = null; success = true; } } return(success); }
/// <summary> /// Starts th probe /// </summary> /// <param name="cancellationToken">A token to monitor for abort requests</param> /// <returns>True if started successfully</returns> public async Task <bool> StartAsync(CancellationToken cancellationToken) { var success = false; if (Task.IsCompleted) { using (await _control.LockAsync(cancellationToken).ConfigureAwait(false)) { if (Task.IsCompleted) { if (_endpoint != null) { try { if (_processingTask == null) { await _processingTask.ConfigureAwait(false); _processingTask = null; } } catch (Exception) { // Suppress errors during cleanup } try { await _endpoint.StopAsync(cancellationToken).ConfigureAwait(false); } catch (Exception) { // Suppress errors to do with stopping the current listener } _endpoint = null; } try { if (_processingTask != null) { _continueProcessing = false; await _processingTask.ConfigureAwait(false); _processingTask = null; } } catch (Exception) { // Suppress exceptions while letting the processor shutdown } _endpoint = new TcpProbeEndpoint(_address, _port, ProbeReceived, ProbeProcessed); _processingTask = ProcessAsync(); success = true; } } } return(success); }