1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Process Monitor - Switched to Processes instead of strings

This commit is contained in:
Diogo Trindade 2021-03-10 16:04:25 +00:00
parent fb51e75b94
commit d4a0b0710c
4 changed files with 32 additions and 14 deletions

View File

@ -46,7 +46,8 @@ namespace Artemis.Core.Services
IPluginManagementService pluginManagementService,
IRgbService rgbService,
IProfileService profileService,
IModuleService moduleService // injected to ensure module priorities get applied
IModuleService moduleService, // injected to ensure module priorities get applied
IProcessMonitorService processMonitorService //injected to keep track of processes
)
{
Kernel = kernel;

View File

@ -1,14 +1,15 @@
using System;
using System.Diagnostics;
namespace Artemis.Core.Services
{
public class ProcessEventArgs : EventArgs
{
public string ProcessName { get; }
public Process Process { get; }
public ProcessEventArgs(string name)
public ProcessEventArgs(Process process)
{
ProcessName = name;
Process = process;
}
}
}

View File

@ -13,6 +13,6 @@ namespace Artemis.Core.Services
event EventHandler<ProcessEventArgs> ProcessStopped;
IEnumerable<string> GetRunningProcesses();
IEnumerable<Process> GetRunningProcesses();
}
}

View File

@ -11,12 +11,12 @@ namespace Artemis.Core.Services
{
private readonly ILogger _logger;
private readonly Timer _processScanTimer;
private IEnumerable<string> _lastScannedProcesses;
private Process[] _lastScannedProcesses;
public ProcessMonitorService(ILogger logger)
{
_logger = logger;
_lastScannedProcesses = Process.GetProcesses().Select(p => p.ProcessName).Distinct().ToArray();
_lastScannedProcesses = Process.GetProcesses().DistinctBy(p => p.ProcessName).ToArray();
_processScanTimer = new Timer(1000);
_processScanTimer.Elapsed += OnTimerElapsed;
_processScanTimer.Start();
@ -26,27 +26,43 @@ namespace Artemis.Core.Services
public event EventHandler<ProcessEventArgs> ProcessStopped;
public IEnumerable<string> GetRunningProcesses()
public IEnumerable<Process> GetRunningProcesses()
{
return _lastScannedProcesses;
}
private void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
var newProcesses = Process.GetProcesses().Select(p => p.ProcessName).Distinct();
foreach (var startedProcess in newProcesses.Except(_lastScannedProcesses))
var newProcesses = Process.GetProcesses().DistinctBy(p => p.ProcessName).ToArray();
foreach (var startedProcess in newProcesses.Except(_lastScannedProcesses, new ProcessComparer()))
{
ProcessStarted?.Invoke(this, new ProcessEventArgs(startedProcess));
_logger.Verbose("Started Process: {startedProcess}", startedProcess);
_logger.Debug("Started Process: {startedProcess}", startedProcess.ProcessName);
}
foreach (var stoppedProcess in _lastScannedProcesses.Except(newProcesses))
foreach (var stoppedProcess in _lastScannedProcesses.Except(newProcesses, new ProcessComparer()))
{
ProcessStopped?.Invoke(this, new ProcessEventArgs(stoppedProcess));
_logger.Verbose("Stopped Process: {stoppedProcess}", stoppedProcess);
_logger.Debug("Stopped Process: {stoppedProcess}", stoppedProcess.ProcessName);
}
_lastScannedProcesses = newProcesses.ToArray();
_lastScannedProcesses = newProcesses;
}
}
internal class ProcessComparer : IEqualityComparer<Process>
{
public bool Equals(Process? x, Process? y)
{
if (x == null && y == null) return true;
if (x == null || y == null) return false;
return x.Id == y.Id && x.ProcessName == y.ProcessName && x.SessionId == y.SessionId;
}
public int GetHashCode(Process obj)
{
if (obj == null) return 0;
return obj.Id;
}
}
}