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

Merge pull request #562 from Artemis-RGB/feature/process-monitor

Core - Added Process Monitor Service
This commit is contained in:
Robert Beekman 2021-03-21 12:36:49 +01:00 committed by GitHub
commit 34b9d69ed8
3 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,21 @@
using System;
using System.Diagnostics;
namespace Artemis.Core.Services
{
/// <summary>
/// Contains data for the ProcessMonitor process events
/// </summary>
public class ProcessEventArgs : EventArgs
{
/// <summary>
/// Gets the process related to the event
/// </summary>
public Process Process { get; }
internal ProcessEventArgs(Process process)
{
Process = process;
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Artemis.Core.Services
{
/// <summary>
/// A service that provides events for started and stopped processes and a list of all running processes.
/// </summary>
public interface IProcessMonitorService : IArtemisService
{
/// <summary>
/// Occurs when a process starts.
/// </summary>
event EventHandler<ProcessEventArgs> ProcessStarted;
/// <summary>
/// Occurs when a process stops.
/// </summary>
event EventHandler<ProcessEventArgs> ProcessStopped;
/// <summary>
/// Returns an enumerable with the processes running on the system.
/// </summary>
IEnumerable<Process> GetRunningProcesses();
}
}

View File

@ -0,0 +1,70 @@
using Serilog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Timers;
namespace Artemis.Core.Services
{
internal class ProcessMonitorService : IProcessMonitorService
{
private readonly ILogger _logger;
private readonly Timer _processScanTimer;
private readonly ProcessComparer _comparer;
private Process[] _lastScannedProcesses;
public ProcessMonitorService(ILogger logger)
{
_logger = logger;
_lastScannedProcesses = Process.GetProcesses();
_processScanTimer = new Timer(1000);
_processScanTimer.Elapsed += OnTimerElapsed;
_processScanTimer.Start();
_comparer = new ProcessComparer();
}
public event EventHandler<ProcessEventArgs>? ProcessStarted;
public event EventHandler<ProcessEventArgs>? ProcessStopped;
public IEnumerable<Process> GetRunningProcesses()
{
return _lastScannedProcesses;
}
private void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
Process[] newProcesses = Process.GetProcesses();
foreach (Process startedProcess in newProcesses.Except(_lastScannedProcesses, _comparer))
{
ProcessStarted?.Invoke(this, new ProcessEventArgs(startedProcess));
_logger.Debug("Started Process: {startedProcess}", startedProcess.ProcessName);
}
foreach (Process stoppedProcess in _lastScannedProcesses.Except(newProcesses, _comparer))
{
ProcessStopped?.Invoke(this, new ProcessEventArgs(stoppedProcess));
_logger.Debug("Stopped Process: {stoppedProcess}", stoppedProcess.ProcessName);
}
_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;
}
}
}