mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 19:36:28 +00:00
Process Monitor - initial POC implementation
This is probably overengineered. The same could be done cross platform only using Processes.GetProcesses. I did it this way because initially i wanted to use Wmi events for process start and stop. This requires admin though, so i decided to not use it.
This commit is contained in:
1 parent
fa26e6b7da
commit
c9ee37ece8
7 files changed
+171
-1
No files matched your search
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
public class ProcessEventArgs : EventArgs
|
||||
{
|
||||
public string ProcessName { get; }
|
||||
|
||||
public ProcessEventArgs(string name)
|
||||
{
|
||||
ProcessName = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
public interface IProcessMonitorService : IArtemisService
|
||||
{
|
||||
void AddProcessWatcher(ProcessWatcher provider);
|
||||
void RemoveProcessWatcher(ProcessWatcher provider);
|
||||
|
||||
IEnumerable<string> GetRunningProcesses();
|
||||
|
||||
event EventHandler<ProcessEventArgs> ProcessStarted;
|
||||
|
||||
event EventHandler<ProcessEventArgs> ProcessStopped;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
public class ProcessMonitorService : IProcessMonitorService
|
||||
{
|
||||
private readonly List<ProcessWatcher> _watchers = new List<ProcessWatcher>();
|
||||
public event EventHandler<ProcessEventArgs> ProcessStarted;
|
||||
public event EventHandler<ProcessEventArgs> ProcessStopped;
|
||||
|
||||
public void AddProcessWatcher(ProcessWatcher watcher)
|
||||
{
|
||||
watcher.ProcessStarted += ProviderOnProcessStarted;
|
||||
watcher.ProcessStopped += ProviderOnProcessStopped;
|
||||
_watchers.Add(watcher);
|
||||
}
|
||||
|
||||
public void RemoveProcessWatcher(ProcessWatcher watcher)
|
||||
{
|
||||
if (!_watchers.Contains(watcher))
|
||||
return;
|
||||
|
||||
_watchers.Remove(watcher);
|
||||
watcher.ProcessStarted -= ProviderOnProcessStarted;
|
||||
watcher.ProcessStopped -= ProviderOnProcessStopped;
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetRunningProcesses()
|
||||
{
|
||||
return _watchers.SelectMany(w => w.GetRunningProcesses());
|
||||
}
|
||||
|
||||
private void ProviderOnProcessStopped(object? sender, ProcessEventArgs e)
|
||||
{
|
||||
ProcessStopped?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private void ProviderOnProcessStarted(object? sender, ProcessEventArgs e)
|
||||
{
|
||||
ProcessStarted?.Invoke(this, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Class that provides info on running processes, including events on event start and stop
|
||||
/// </summary>
|
||||
public abstract class ProcessWatcher
|
||||
{
|
||||
public event EventHandler<ProcessEventArgs> ProcessStarted;
|
||||
public event EventHandler<ProcessEventArgs> ProcessStopped;
|
||||
|
||||
public abstract IEnumerable<string> GetRunningProcesses();
|
||||
|
||||
public virtual void OnProcessStarted(string processName)
|
||||
{
|
||||
ProcessStarted?.Invoke(this, new ProcessEventArgs(processName));
|
||||
}
|
||||
|
||||
public virtual void OnProcessStopped(string processName)
|
||||
{
|
||||
ProcessStopped?.Invoke(this, new ProcessEventArgs(processName));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user