diff --git a/UI/AmagnoVirtualPrinter.ProgressInfo.Lib/ProgressInfoProcessManager.cs b/UI/AmagnoVirtualPrinter.ProgressInfo.Lib/ProgressInfoProcessManager.cs index 3416631..ef9d29c 100644 --- a/UI/AmagnoVirtualPrinter.ProgressInfo.Lib/ProgressInfoProcessManager.cs +++ b/UI/AmagnoVirtualPrinter.ProgressInfo.Lib/ProgressInfoProcessManager.cs @@ -1,13 +1,14 @@ -using JetBrains.Annotations; - -using System; +using System; +using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using AmagnoVirtualPrinter.Agent.Core.Interfaces; +using AmagnoVirtualPrinter.Logging; using AmagnoVirtualPrinter.ProgressInfo.Lib.Interfaces; using AmagnoVirtualPrinter.Utils; +using JetBrains.Annotations; namespace AmagnoVirtualPrinter.ProgressInfo.Lib { @@ -16,6 +17,14 @@ public class ProgressInfoProcessManager : IProgressInfoProcessManager { private const string ProcessName = "AmagnoPrinterAgentProgress"; + [NotNull] + private readonly IVirtualPrinterLogger _logger; + + public ProgressInfoProcessManager([NotNull]IVirtualPrinterLogger logger) + { + _logger = logger; + } + public bool IsRunning() { return Process.GetProcesses().Any(pList => pList.ProcessName.Contains(ProcessName)); @@ -37,10 +46,36 @@ public void Stop() { var processes = Process.GetProcesses().Where(pList => pList.ProcessName.Contains(ProcessName)); - foreach(var process in processes) + foreach (var process in processes) + { + KillProcess(process); + } + } + + private void KillProcess([NotNull]Process process) + { + try { process.Kill(); } + catch (Win32Exception exception) + { + // The associated process could not be terminated. -or- + // The process is terminating. -or- + // The associated process is a Win16 executable. + LogWarn(exception, "Failed to kill process while printing"); + } + catch (InvalidOperationException exception) + { + // The process has already exited. -or- + // There is no process associated. + LogWarn(exception, "Failed to kill process while printing"); + } + } + + private void LogWarn(Exception exception, string message) + { + _logger.Warn("{message}:\r\nException: {exception}", message, exception); } } -} +} \ No newline at end of file