private static async void ProducerLoop() { try { while (!_shutdown) { try { using (_pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.InOut, 10, PipeTransmissionMode.Message, PipeOptions.Asynchronous)) { await _pipeServer.WaitForConnectionAsync(_cancellationSource.Token); if (!_pipeServer.IsConnected || _shutdown) { return; } StreamString io = new StreamString(_pipeServer); while (_pipeServer.IsConnected) { string commandLine = io.ReadString(); if (commandLine == null || _shutdown) { break; } Debug.WriteLine("Command received via NamedPipe: " + commandLine); if (!string.IsNullOrWhiteSpace(commandLine)) { ExternalCommand command = new ExternalCommand(commandLine); CommandLineQueue.Enqueue(command); var result = command.WaitForResult(TimeSpan.FromMilliseconds(2000)); io.WriteString((result.Success ? "OK" : "FAIL") + ":" + result.Message); } } } } catch (Exception ex) { Debug.WriteLine("InstanceHandler.ProducerLoop: " + ex.Message); } } } catch (OperationCanceledException) { } catch (ThreadAbortException) { } }