From e396fbfb6062052784559ef61db98cc3a28196fc Mon Sep 17 00:00:00 2001 From: Simon G Date: Fri, 23 Apr 2021 18:58:50 +0200 Subject: [PATCH] - add processStarted and processExited event --- Lib.ProcessManaging/Interfaces/IProcessManager.cs | 3 +++ Lib.ProcessManaging/ProcessManager.cs | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Lib.ProcessManaging/Interfaces/IProcessManager.cs b/Lib.ProcessManaging/Interfaces/IProcessManager.cs index 153e418..7d1c64b 100644 --- a/Lib.ProcessManaging/Interfaces/IProcessManager.cs +++ b/Lib.ProcessManaging/Interfaces/IProcessManager.cs @@ -12,5 +12,8 @@ namespace Lib.ProcessManaging.Interfaces List? Processes { get; } void Initialize(); + + event EventHandler? ProcessStarted; + event EventHandler? ProcessExited; } } \ No newline at end of file diff --git a/Lib.ProcessManaging/ProcessManager.cs b/Lib.ProcessManaging/ProcessManager.cs index fdf3822..d8360ad 100644 --- a/Lib.ProcessManaging/ProcessManager.cs +++ b/Lib.ProcessManaging/ProcessManager.cs @@ -2,6 +2,7 @@ // Created: 2021-04-23 // Copyright(c) 2021 SimonG. All Rights Reserved. +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -55,12 +56,15 @@ namespace Lib.ProcessManaging if (Processes.Any(p => p.EqualsProcess(process))) continue; - Processes.Add(_observedProcessFactory.Create(process)); + IObservedProcess observedProcess = _observedProcessFactory.Create(process); + Processes.Add(observedProcess); + ProcessStarted?.Invoke(this, observedProcess); } foreach (var observedProcess in Processes.ToList().Where(p => !Process.GetProcesses().Any(p.EqualsProcess))) { Processes.Remove(observedProcess); + ProcessExited?.Invoke(this, observedProcess); //TestMe: is the passed process still usable? (disposed directly afterwards) observedProcess.Dispose(); } @@ -68,6 +72,9 @@ namespace Lib.ProcessManaging } } + public event EventHandler? ProcessStarted; + public event EventHandler? ProcessExited; + public void Dispose() { Processes.ForEach(p => p.Dispose());