1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-31 17:53:32 +00:00

Process Monitor - documentation and code style

This commit is contained in:
Diogo Trindade 2021-03-19 16:16:09 +00:00
parent a21db5eaa4
commit be11439b21
3 changed files with 28 additions and 8 deletions

View File

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

View File

@ -7,12 +7,24 @@ using System.Threading.Tasks;
namespace Artemis.Core.Services 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 public interface IProcessMonitorService : IArtemisService
{ {
/// <summary>
/// Occurs when a process starts.
/// </summary>
event EventHandler<ProcessEventArgs> ProcessStarted; event EventHandler<ProcessEventArgs> ProcessStarted;
/// <summary>
/// Occurs when a process stops.
/// </summary>
event EventHandler<ProcessEventArgs> ProcessStopped; event EventHandler<ProcessEventArgs> ProcessStopped;
/// <summary>
/// Returns an enumerable with the processes running on the system.
/// </summary>
IEnumerable<Process> GetRunningProcesses(); IEnumerable<Process> GetRunningProcesses();
} }
} }

View File

@ -7,24 +7,26 @@ using System.Timers;
namespace Artemis.Core.Services namespace Artemis.Core.Services
{ {
public class ProcessMonitorService : IProcessMonitorService internal class ProcessMonitorService : IProcessMonitorService
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly Timer _processScanTimer; private readonly Timer _processScanTimer;
private readonly ProcessComparer _comparer;
private Process[] _lastScannedProcesses; private Process[] _lastScannedProcesses;
public ProcessMonitorService(ILogger logger) public ProcessMonitorService(ILogger logger)
{ {
_logger = logger; _logger = logger;
_lastScannedProcesses = Process.GetProcesses().DistinctBy(p => p.ProcessName).ToArray(); _lastScannedProcesses = Process.GetProcesses();
_processScanTimer = new Timer(1000); _processScanTimer = new Timer(1000);
_processScanTimer.Elapsed += OnTimerElapsed; _processScanTimer.Elapsed += OnTimerElapsed;
_processScanTimer.Start(); _processScanTimer.Start();
_comparer = new ProcessComparer();
} }
public event EventHandler<ProcessEventArgs> ProcessStarted; public event EventHandler<ProcessEventArgs>? ProcessStarted;
public event EventHandler<ProcessEventArgs> ProcessStopped; public event EventHandler<ProcessEventArgs>? ProcessStopped;
public IEnumerable<Process> GetRunningProcesses() public IEnumerable<Process> GetRunningProcesses()
{ {
@ -33,14 +35,14 @@ namespace Artemis.Core.Services
private void OnTimerElapsed(object sender, ElapsedEventArgs e) private void OnTimerElapsed(object sender, ElapsedEventArgs e)
{ {
var newProcesses = Process.GetProcesses().DistinctBy(p => p.ProcessName).ToArray(); Process[] newProcesses = Process.GetProcesses();
foreach (var startedProcess in newProcesses.Except(_lastScannedProcesses, new ProcessComparer())) foreach (Process startedProcess in newProcesses.Except(_lastScannedProcesses, _comparer))
{ {
ProcessStarted?.Invoke(this, new ProcessEventArgs(startedProcess)); ProcessStarted?.Invoke(this, new ProcessEventArgs(startedProcess));
_logger.Debug("Started Process: {startedProcess}", startedProcess.ProcessName); _logger.Debug("Started Process: {startedProcess}", startedProcess.ProcessName);
} }
foreach (var stoppedProcess in _lastScannedProcesses.Except(newProcesses, new ProcessComparer())) foreach (Process stoppedProcess in _lastScannedProcesses.Except(newProcesses, _comparer))
{ {
ProcessStopped?.Invoke(this, new ProcessEventArgs(stoppedProcess)); ProcessStopped?.Invoke(this, new ProcessEventArgs(stoppedProcess));
_logger.Debug("Stopped Process: {stoppedProcess}", stoppedProcess.ProcessName); _logger.Debug("Stopped Process: {stoppedProcess}", stoppedProcess.ProcessName);