diff --git a/src/FluffySpoon.Ngrok/NgrokProcess.cs b/src/FluffySpoon.Ngrok/NgrokProcess.cs index 57294d0..a70449b 100644 --- a/src/FluffySpoon.Ngrok/NgrokProcess.cs +++ b/src/FluffySpoon.Ngrok/NgrokProcess.cs @@ -24,13 +24,19 @@ public void Start() { var processInformation = GetProcessStartInfo(); - var existingProcess = Process.GetProcessesByName(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? - "Ngrok" : - "ngrok"); - if (existingProcess.Any()) + var existingProcesses = Process + .GetProcessesByName(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Ngrok" : "ngrok") + .ToArray(); + if (existingProcesses.Any()) { _logger.LogDebug("Ngrok process ({ProcessName}) is already running", processInformation.FileName); - SetProcess(existingProcess.First()); + SetProcess(existingProcesses.First()); + + foreach (var existingProcess in existingProcesses.Skip(1)) + { + existingProcess.Kill(); + } + return; } @@ -55,7 +61,7 @@ private void ProcessErrorDataReceived(object? sender, DataReceivedEventArgs e) { if (string.IsNullOrWhiteSpace(e.Data)) return; - + _logger.LogError("{Error}", e.Data); } @@ -67,7 +73,7 @@ private ProcessWindowStyle GetProcessWindowStyle() private ProcessStartInfo GetProcessStartInfo() { var processStartInfo = new ProcessStartInfo( - NgrokDownloader.GetExecutableFileName(), + NgrokDownloader.GetExecutableFileName(), "start --none") { CreateNoWindow = true, @@ -82,14 +88,14 @@ public void Stop() { _logger.LogInformation("Stopping ngrok process"); - if (_process == null) + if (_process == null) return; - + _process.ErrorDataReceived -= ProcessErrorDataReceived; - + _process.Kill(); _process.WaitForExit(); - + _process = null; } } \ No newline at end of file