static async Task Main(string[] args) { bool run = true; Console.CancelKeyPress += (s, e) => { e.Cancel = true; run = false; }; var computer = new Computer { IsCpuEnabled = true, IsGpuEnabled = true }; computer.Open(); var cpu = computer.Hardware.First(hw => hw.HardwareType == HardwareType.Cpu); cpu.Update(); foreach (var sens in cpu.Sensors.Where(s => s.SensorType == SensorType.Temperature)) { Console.WriteLine($"{sens.Name} = {sens.Value}"); } computer.Close(); using (HwMonitor hw = new HwMonitor()) using (Aero aero = new Aero()) { //Random rng = new Random(); //IDirectFanSpeedController fans = (IDirectFanSpeedController)aero.Fans; while (run) { hw.Update(); double wmiCpuTemp = await aero.Cpu.GetTemperatureAsync(); double monCpuTemp = hw.CpuTemperature; double monGpuTemp = hw.GpuTemperature; Console.Write($"CPU {wmiCpuTemp:F1}°C \t {monCpuTemp:F1}°C \t GPU {monGpuTemp:F1}°C "); Console.CursorLeft = 0; //fans.SetFixed(0.0 + rng.NextDouble() * 0.3); await Task.Delay(500); } } }
private void InitStats() { uint cpuUsage = 0; long memoryTotal = 0; long memoryUsed = 0; bool firstRun = false; Task calculationT = new Task(() => { while (true) { cpuUsage = HwMonitor.GetCoreUsage(); Interlocked.Exchange(ref memoryTotal, (long)HwMonitor.GetTotalMemory()); Interlocked.Exchange(ref memoryUsed, (long)HwMonitor.GetUsedMemory()); firstRun = true; } }, TaskCreationOptions.LongRunning); Task sendingT = new Task(() => { while (!firstRun) { Thread.Sleep(1000); } while (true) { SystemMsg sysMsg = new SystemMsg(); StatMsg statMsg = new StatMsg(); statMsg.CpuUsage = cpuUsage; statMsg.MemoryTotal = (ulong)Interlocked.Read(ref memoryTotal); statMsg.MemoryUsed = (ulong)Interlocked.Read(ref memoryUsed); sysMsg.StatMsg = statMsg; MainMessage msg = new MainMessage(); msg.SystemMsg = sysMsg; Thread.Sleep((int)STATS_INTERVAL_MS); _api.OpenAPI.Networking.SendAsync(msg, _api.OpenAPI.Config.MainServer, (_) => { }, (err) => { _log.Error("There was an error during status sending to Main Server."); Environment.Exit(1); }); } }, TaskCreationOptions.LongRunning); calculationT.Start(); sendingT.Start(); }