using System; using System.Diagnostics; using System.Linq; using System.Management; using System.Threading.Tasks; namespace WoRCP { class ResourceReader { //Main #region Variables public static double CPU = 0; public static double Mem = 0; public static double DiskR = 0; public static double DiskW = 0; public static double Temprature = 0; private static PerformanceCounter CPUUsage; private static PerformanceCounter Memory; private static PerformanceCounter DiskRead; private static PerformanceCounter DiskWrite; private static ManagementObject Temp; private static ManagementObjectSearcher searcher; #endregion //Methods #region Defining the counters public static async void DefineCounters() { if (!Configuration.CountersDefined) { Program.Log("[Info] Defining resource counters... (This process might take a while)"); await Task.Run(() => { CPUUsage = new PerformanceCounter("Processor", "% Processor Time", "_Total", true); Memory = new PerformanceCounter("Memory", "Available MBytes", true); DiskRead = new PerformanceCounter("LogicalDisk", "Disk Read Bytes/Sec", "_Total", true); DiskWrite = new PerformanceCounter("LogicalDisk", "Disk Write Bytes/Sec", "_Total", true); searcher = new ManagementObjectSearcher(@"root\CIMV2", "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation"); Configuration.CountersDefined = true; Program.Log("[Info] Resource counters defined."); }); } else { Program.Log("[Warn] Resource counters are already defined."); } } #endregion #region Reading public static void Read() { try { CPU = Math.Round(CPUUsage.NextValue()); Mem = Configuration.Totalmemory - Math.Round(Memory.NextValue() / 1024, 1); DiskR = Math.Round(DiskRead.NextValue() / 1024 / 1024, 1); DiskW = Math.Round(DiskWrite.NextValue() / 1024 / 1024, 1); Temp = searcher.Get().OfType().First(); Temprature = (Convert.ToDouble(Temp.GetPropertyValue("HighPrecisionTemperature").ToString()) - 2732) / 10; } catch { Program.Log("[Error] Resource counters are undefined, Unable to read."); } } #endregion } }