From 468a2a75df4ee746bee3252e13cdcf0a9a7c5f30 Mon Sep 17 00:00:00 2001 From: Ak <79897291+0vm@users.noreply.github.com> Date: Sat, 22 Jul 2023 18:43:53 +1000 Subject: [PATCH] custom multiple programs --- Program.cs | 111 ++++++++++++++++++++++++++--------------------------- 1 file changed, 55 insertions(+), 56 deletions(-) diff --git a/Program.cs b/Program.cs index afe311c..d67a0c7 100644 --- a/Program.cs +++ b/Program.cs @@ -4,9 +4,7 @@ using System.Linq; using System.Management; using System.Runtime.InteropServices; -using System.Text; using System.Threading; -using System.Threading.Tasks; namespace RAMLIMITER { @@ -17,6 +15,61 @@ class Program [DllImport("kernel32.dll")] static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max); + // Method to get all processes matching the custom process name + public static List GetCustom(string processName) + { + return Process.GetProcessesByName(processName).ToList(); + } + + // Method to limit RAM usage of multiple custom processes + static void CustomRamLimiter(int min, int max) + { + Console.WriteLine("Type Process Names Separated by Commas (e.g., chrome,obs,discord):"); + string processNamesInput = Console.ReadLine(); + List processNames = processNamesInput.Split(',').Select(p => p.Trim().ToLower()).ToList(); + + List processesToLimit = new List(); + foreach (var processName in processNames) + { + processesToLimit.AddRange(GetCustom(processName)); + } + + foreach (var process in processesToLimit) + { + new Thread(() => + { + Thread.CurrentThread.IsBackground = true; + while (!process.HasExited) + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + if (Environment.OSVersion.Platform == PlatformID.Win32NT) + { + SetProcessWorkingSetSize(process.Handle, min, max); + } + + var wmiObject = new ManagementObjectSearcher("select * from Win32_OperatingSystem"); + + var memoryValues = wmiObject.Get().Cast().Select(mo => new + { + FreePhysicalMemory = Double.Parse(mo["FreePhysicalMemory"].ToString()), + TotalVisibleMemorySize = Double.Parse(mo["TotalVisibleMemorySize"].ToString()) + }).FirstOrDefault(); + + if (memoryValues != null) + { + var percent = ((memoryValues.TotalVisibleMemorySize - memoryValues.FreePhysicalMemory) / memoryValues.TotalVisibleMemorySize) * 100; + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine(process.ProcessName + ": Total RAM usage: {0}", percent); + Thread.Sleep(3000); + } + } + }).Start(); + } + + Thread.Sleep(-1); + } + // Method to get the process ID of Discord public static int GetDiscord() { @@ -49,22 +102,6 @@ public static int GetChrome() return chromeId; } - // Method to get the process ID of a custom process - public static int GetCustom() - { - int OBSId = -1; - long workingSet2 = 0; - foreach (Process OBS in Process.GetProcessesByName(variableger)) - { - if (OBS.WorkingSet64 > workingSet2) - { - workingSet2 = OBS.WorkingSet64; - OBSId = OBS.Id; - } - } - return OBSId; - } - // Method to get the process ID of OBS public static int GetOBS() { @@ -270,44 +307,6 @@ static void OBSRamLimiter(int min, int max) } } - // Method to limit RAM usage of a custom process - static void CustomRamLimiter(int min, int max) - { - Console.WriteLine("Type Process Name Like Chrome or OBS"); - variableger = Console.ReadLine(); - - while (GetCustom() != -1) - { - if (GetCustom() != -1) - { - GC.Collect(); - GC.WaitForPendingFinalizers(); - - if (Environment.OSVersion.Platform == PlatformID.Win32NT) - { - SetProcessWorkingSetSize(Process.GetProcessById(GetCustom()).Handle, min, max); - } - - var wmiObject = new ManagementObjectSearcher("select * from Win32_OperatingSystem"); - - var memoryValues = wmiObject.Get().Cast().Select(mo => new { - FreePhysicalMemory = Double.Parse(mo["FreePhysicalMemory"].ToString()), - TotalVisibleMemorySize = Double.Parse(mo["TotalVisibleMemorySize"].ToString()) - }).FirstOrDefault(); - - if (memoryValues != null) - { - var percent = ((memoryValues.TotalVisibleMemorySize - memoryValues.FreePhysicalMemory) / memoryValues.TotalVisibleMemorySize) * 100; - Console.ForegroundColor = ConsoleColor.Cyan; - Console.WriteLine(variableger + ": Total RAM usage: {0}", percent); - Thread.Sleep(3000); - } - - Thread.Sleep(1); - } - } - } - static void Main(string[] args) { new Thread(() => // taken from pinger, originally from zf9