1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-12 13:28:33 +00:00

Core - Further reduced allocations in process monitor checking

This commit is contained in:
Diogo Trindade 2023-09-04 15:55:27 +01:00
parent d26f62223b
commit 8a1b302e48
2 changed files with 35 additions and 10 deletions

View File

@ -38,16 +38,7 @@ public class ProcessActivationRequirement : IModuleActivationRequirement
/// <inheritdoc />
public bool Evaluate()
{
if (!ProcessMonitor.IsStarted || (ProcessName == null && Location == null))
return false;
IEnumerable<ProcessInfo> processes = ProcessMonitor.Processes;
if (ProcessName != null)
processes = processes.Where(p => string.Equals(p.ProcessName, ProcessName, StringComparison.InvariantCultureIgnoreCase));
if (Location != null)
processes = processes.Where(p => string.Equals(Path.GetDirectoryName(p.Executable), Location, StringComparison.InvariantCultureIgnoreCase));
return processes.Any();
return ProcessMonitor.IsProcessRunning(ProcessName, Location);
}
/// <inheritdoc />

View File

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Threading;
namespace Artemis.Core.Services;
@ -99,6 +101,23 @@ public static partial class ProcessMonitor
FreeBuffer();
}
}
/// <summary>
/// Returns whether the specified process is running
/// </summary>
/// <param name="processName">The name of the process to check</param>
/// <param name="processLocation">The location of where the process must be running from (optional)</param>
/// <returns></returns>
public static bool IsProcessRunning(string? processName = null, string? processLocation = null)
{
if (!IsStarted || (processName == null && processLocation == null))
return false;
lock (LOCK)
{
return _processes.Values.Any(x => Match(x, processName, processLocation));
}
}
// ReSharper disable once SuggestBaseTypeForParameter
private static void HandleStoppedProcesses(HashSet<int> currentProcessIds)
@ -112,6 +131,21 @@ public static partial class ProcessMonitor
OnProcessStopped(info);
}
}
private static bool Match(ProcessInfo info, string? processName, string? processLocation)
{
if (processName != null && processLocation != null)
return string.Equals(info.ProcessName, processName, StringComparison.InvariantCultureIgnoreCase) &&
string.Equals(Path.GetDirectoryName(info.Executable), processLocation, StringComparison.InvariantCultureIgnoreCase);
if (processName != null)
return string.Equals(info.ProcessName, processName, StringComparison.InvariantCultureIgnoreCase);
if (processLocation != null)
return string.Equals(Path.GetDirectoryName(info.Executable), processLocation, StringComparison.InvariantCultureIgnoreCase);
return false;
}
private static void OnProcessStarted(ProcessInfo processInfo)
{