From c8e8bfb078dac87d5cea6b897a3b3193c05f291a Mon Sep 17 00:00:00 2001 From: Simon G Date: Fri, 23 Apr 2021 16:22:39 +0200 Subject: [PATCH] - continue implementing process managing --- .../Factories/IObservedProcessFactory.cs | 14 ++++ .../Interfaces/IObservedProcess.cs | 10 ++- .../Interfaces/IProcessManager.cs | 8 +-- Lib.ProcessManaging/ObservedProcess.cs | 24 +++++++ Lib.ProcessManaging/ProcessManager.cs | 71 +++++++++++++++++++ Mystify/App.axaml.cs | 9 ++- Mystify/Bootstrapper.cs | 3 +- .../Installers/ProcessManagingInstaller.cs | 25 +++++++ Mystify/Mystify.csproj | 1 + 9 files changed, 157 insertions(+), 8 deletions(-) create mode 100644 Lib.ProcessManaging/Factories/IObservedProcessFactory.cs create mode 100644 Lib.ProcessManaging/ObservedProcess.cs create mode 100644 Lib.ProcessManaging/ProcessManager.cs create mode 100644 Mystify/Installers/ProcessManagingInstaller.cs diff --git a/Lib.ProcessManaging/Factories/IObservedProcessFactory.cs b/Lib.ProcessManaging/Factories/IObservedProcessFactory.cs new file mode 100644 index 0000000..ce7cdf5 --- /dev/null +++ b/Lib.ProcessManaging/Factories/IObservedProcessFactory.cs @@ -0,0 +1,14 @@ +// Author: Gockner, Simon +// Created: 2021-04-23 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using System.Diagnostics; +using Lib.ProcessManaging.Interfaces; + +namespace Lib.ProcessManaging.Factories +{ + public interface IObservedProcessFactory + { + IObservedProcess Create(Process process); + } +} \ No newline at end of file diff --git a/Lib.ProcessManaging/Interfaces/IObservedProcess.cs b/Lib.ProcessManaging/Interfaces/IObservedProcess.cs index 1900151..c6cce23 100644 --- a/Lib.ProcessManaging/Interfaces/IObservedProcess.cs +++ b/Lib.ProcessManaging/Interfaces/IObservedProcess.cs @@ -2,10 +2,16 @@ // Created: 2021-04-16 // Copyright(c) 2021 SimonG. All Rights Reserved. +using System; +using System.Diagnostics; + namespace Lib.ProcessManaging.Interfaces { - public interface IObservedProcess + public interface IObservedProcess : IDisposable { - + string Name { get; } + int Id { get; } + + bool EqualsProcess(Process process); } } \ No newline at end of file diff --git a/Lib.ProcessManaging/Interfaces/IProcessManager.cs b/Lib.ProcessManaging/Interfaces/IProcessManager.cs index 8bb3683..153e418 100644 --- a/Lib.ProcessManaging/Interfaces/IProcessManager.cs +++ b/Lib.ProcessManaging/Interfaces/IProcessManager.cs @@ -2,15 +2,15 @@ // Created: 2021-04-16 // Copyright(c) 2021 SimonG. All Rights Reserved. +using System; using System.Collections.Generic; -using System.Threading.Tasks; namespace Lib.ProcessManaging.Interfaces { - public interface IProcessManager + public interface IProcessManager : IDisposable { - List Processes { get; } + List? Processes { get; } - Task Initialize(); + void Initialize(); } } \ No newline at end of file diff --git a/Lib.ProcessManaging/ObservedProcess.cs b/Lib.ProcessManaging/ObservedProcess.cs new file mode 100644 index 0000000..192cc0c --- /dev/null +++ b/Lib.ProcessManaging/ObservedProcess.cs @@ -0,0 +1,24 @@ +// Author: Gockner, Simon +// Created: 2021-04-23 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using System; +using System.Diagnostics; +using Lib.ProcessManaging.Interfaces; + +namespace Lib.ProcessManaging +{ + public class ObservedProcess : IObservedProcess + { + private readonly Process _process; + + public ObservedProcess(Process process) => _process = process; + + public string Name => _process.ProcessName; + public int Id => _process.Id; + + public bool EqualsProcess(Process process) => _process.Id == process.Id; + + public void Dispose() => _process.Dispose(); + } +} \ No newline at end of file diff --git a/Lib.ProcessManaging/ProcessManager.cs b/Lib.ProcessManaging/ProcessManager.cs new file mode 100644 index 0000000..51bb5d6 --- /dev/null +++ b/Lib.ProcessManaging/ProcessManager.cs @@ -0,0 +1,71 @@ +// Author: Gockner, Simon +// Created: 2021-04-23 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading; +using Lib.ProcessManaging.Factories; +using Lib.ProcessManaging.Interfaces; + +namespace Lib.ProcessManaging +{ + public class ProcessManager : IProcessManager + { + private readonly IObservedProcessFactory _observedProcessFactory; + + private Thread? _monitorThread; + + public ProcessManager(IObservedProcessFactory observedProcessFactory) + { + _observedProcessFactory = observedProcessFactory; + Processes = new List(); + } + + public List Processes { get; } + + public void Initialize() + { + // using ManualResetEvent monitorReadyEvent = new(false); + _monitorThread = new Thread(Monitor) + { + Name = "Process Monitor", + IsBackground = true + }; + + _monitorThread.Start(); + // monitorReadyEvent.WaitOne(); + } + + private void Monitor(object? state) + { + // if (state is not ManualResetEvent monitorReadyEvent) + // return; + // + // monitorReadyEvent.Set(); + + foreach (var process in Process.GetProcesses()) + { + if (Processes.Any(p => p.EqualsProcess(process))) + continue; + + Processes.Add(_observedProcessFactory.Create(process)); + } + + foreach (IObservedProcess observedProcess in Processes.ToList().Where(p => !Process.GetProcesses().Any(p.EqualsProcess))) + { + Processes.Remove(observedProcess); + observedProcess.Dispose(); + } + + Thread.Sleep(1000); + } + + public void Dispose() + { + Processes.ForEach(p => p.Dispose()); + _monitorThread = null; + } + } +} \ No newline at end of file diff --git a/Mystify/App.axaml.cs b/Mystify/App.axaml.cs index 2936f4b..85ac5dc 100644 --- a/Mystify/App.axaml.cs +++ b/Mystify/App.axaml.cs @@ -12,6 +12,7 @@ using Lib.Logging.Interfaces; using Lib.Logging.Loggers.Factories; using Lib.Logging.Loggers.Interfaces; using Lib.NotifyIcon; +using Lib.ProcessManaging.Interfaces; using LightweightIocContainer.Interfaces; using Mystify.ViewModels; using Mystify.Views; @@ -25,6 +26,8 @@ namespace Mystify private ILog? _log; private ILoggerInitializer? _loggerInitializer; + + private IProcessManager? _processManager; private INotifyIcon? _notifyIcon; @@ -50,6 +53,9 @@ namespace Mystify await _log.WriteLogHeader(); + _processManager = _kernel.Resolve(); + _processManager.Initialize(); + _mainModel = _kernel.Resolve(); _mainWindow = new MainWindow(); _mainWindowViewModel = new MainWindowViewModel(_mainModel, _mainWindow); @@ -92,7 +98,8 @@ namespace Mystify _mainWindow?.Close(); _mainModel?.Dispose(); _notifyIcon?.Remove(); - + _processManager?.Dispose(); + if (_log != null) await _log.DisposeAsync(); diff --git a/Mystify/Bootstrapper.cs b/Mystify/Bootstrapper.cs index f861c73..39534a5 100644 --- a/Mystify/Bootstrapper.cs +++ b/Mystify/Bootstrapper.cs @@ -17,6 +17,7 @@ namespace Mystify new DriverInstaller(), new LoggingInstaller(), new MidiInstaller(), - new NotifyIconInstaller()); + new NotifyIconInstaller(), + new ProcessManagingInstaller()); } } \ No newline at end of file diff --git a/Mystify/Installers/ProcessManagingInstaller.cs b/Mystify/Installers/ProcessManagingInstaller.cs new file mode 100644 index 0000000..6b74195 --- /dev/null +++ b/Mystify/Installers/ProcessManagingInstaller.cs @@ -0,0 +1,25 @@ +// Author: Gockner, Simon +// Created: 2021-04-23 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using Lib.ProcessManaging; +using Lib.ProcessManaging.Factories; +using Lib.ProcessManaging.Interfaces; +using LightweightIocContainer; +using LightweightIocContainer.Interfaces; +using LightweightIocContainer.Interfaces.Installers; + +namespace Mystify.Installers +{ + public class ProcessManagingInstaller : IIocInstaller + { + public void Install(IIocContainer container) + { + container.Register(Lifestyle.Singleton); + container.Register(); + + //factories + container.RegisterFactory(); + } + } +} \ No newline at end of file diff --git a/Mystify/Mystify.csproj b/Mystify/Mystify.csproj index e1312af..cfbf69e 100644 --- a/Mystify/Mystify.csproj +++ b/Mystify/Mystify.csproj @@ -22,6 +22,7 @@ +