1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +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
{
/// <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; }
public ProcessEventArgs(Process process)
internal ProcessEventArgs(Process process)
{
Process = process;
}

View File

@ -7,12 +7,24 @@ 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

@ -7,24 +7,26 @@ using System.Timers;
namespace Artemis.Core.Services
{
public class ProcessMonitorService : IProcessMonitorService
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().DistinctBy(p => p.ProcessName).ToArray();
_lastScannedProcesses = Process.GetProcesses();
_processScanTimer = new Timer(1000);
_processScanTimer.Elapsed += OnTimerElapsed;
_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()
{
@ -33,14 +35,14 @@ namespace Artemis.Core.Services
private void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
var newProcesses = Process.GetProcesses().DistinctBy(p => p.ProcessName).ToArray();
foreach (var startedProcess in newProcesses.Except(_lastScannedProcesses, new ProcessComparer()))
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 (var stoppedProcess in _lastScannedProcesses.Except(newProcesses, new ProcessComparer()))
foreach (Process stoppedProcess in _lastScannedProcesses.Except(newProcesses, _comparer))
{
ProcessStopped?.Invoke(this, new ProcessEventArgs(stoppedProcess));
_logger.Debug("Stopped Process: {stoppedProcess}", stoppedProcess.ProcessName);