Code style - Use file scoped namespaces

Code style - Ran code cleanup
This commit is contained in:
Robert committed 2022-08-21 11:36:15 +02:00
1 parent e68e16df4d
commit f6090dc296
691 files changed
+37301 -38119

No files matched your search

@@ -1,21 +1,20 @@
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; }
namespace Artemis.Core.Services;
internal ProcessEventArgs(Process process)
{
Process = process;
}
/// <summary>
/// Contains data for the ProcessMonitor process events
/// </summary>
public class ProcessEventArgs : EventArgs
{
internal ProcessEventArgs(Process process)
{
Process = process;
}
}
/// <summary>
/// Gets the process related to the event
/// </summary>
public Process Process { get; }
}
@@ -1,30 +1,26 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
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
{
/// <summary>
/// A service that provides events for started and stopped processes and a list of all running processes.
/// Occurs when a process starts.
/// </summary>
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;
/// <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();
}
}
/// <summary>
/// Returns an enumerable with the processes running on the system.
/// </summary>
IEnumerable<Process> GetRunningProcesses();
}
@@ -1,71 +1,70 @@
using Serilog;
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Timers;
using Artemis.Core.Modules;
using Serilog;
namespace Artemis.Core.Services
namespace Artemis.Core.Services;
internal class ProcessMonitorService : IProcessMonitorService
{
internal class ProcessMonitorService : IProcessMonitorService
private readonly ProcessComparer _comparer;
private readonly ILogger _logger;
private Process[] _lastScannedProcesses;
public ProcessMonitorService(ILogger logger)
{
private readonly ILogger _logger;
private readonly ProcessComparer _comparer;
private Process[] _lastScannedProcesses;
_logger = logger;
_lastScannedProcesses = Process.GetProcesses();
Timer processScanTimer = new(1000);
processScanTimer.Elapsed += OnTimerElapsed;
processScanTimer.Start();
_comparer = new ProcessComparer();
public ProcessMonitorService(ILogger logger)
{
_logger = logger;
_lastScannedProcesses = Process.GetProcesses();
Timer processScanTimer = new(1000);
processScanTimer.Elapsed += OnTimerElapsed;
processScanTimer.Start();
_comparer = new ProcessComparer();
ProcessActivationRequirement.ProcessMonitorService = this;
}
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.Verbose("Started Process: {startedProcess}", startedProcess.ProcessName);
}
foreach (Process stoppedProcess in _lastScannedProcesses.Except(newProcesses, _comparer))
{
ProcessStopped?.Invoke(this, new ProcessEventArgs(stoppedProcess));
_logger.Verbose("Stopped Process: {stoppedProcess}", stoppedProcess.ProcessName);
}
_lastScannedProcesses = newProcesses;
}
ProcessActivationRequirement.ProcessMonitorService = this;
}
internal class ProcessComparer : IEqualityComparer<Process>
private void OnTimerElapsed(object? sender, ElapsedEventArgs e)
{
public bool Equals(Process? x, Process? y)
Process[] newProcesses = Process.GetProcesses();
foreach (Process startedProcess in newProcesses.Except(_lastScannedProcesses, _comparer))
{
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;
ProcessStarted?.Invoke(this, new ProcessEventArgs(startedProcess));
_logger.Verbose("Started Process: {startedProcess}", startedProcess.ProcessName);
}
public int GetHashCode(Process? obj)
foreach (Process stoppedProcess in _lastScannedProcesses.Except(newProcesses, _comparer))
{
if (obj == null) return 0;
return obj.Id;
ProcessStopped?.Invoke(this, new ProcessEventArgs(stoppedProcess));
_logger.Verbose("Stopped Process: {stoppedProcess}", stoppedProcess.ProcessName);
}
_lastScannedProcesses = newProcesses;
}
public event EventHandler<ProcessEventArgs>? ProcessStarted;
public event EventHandler<ProcessEventArgs>? ProcessStopped;
public IEnumerable<Process> GetRunningProcesses()
{
return _lastScannedProcesses;
}
}
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;
}
}