public void RealTimeMonitoring() { Task.Run(() => { try { // Sleep的时间间隔 int interval = 2000; PerformanceCounter totalcpu = new PerformanceCounter("Processor", "% Processor Time", "_Total"); SystemInfo sys = new SystemInfo(); const int KB_DIV = 1024; const int MB_DIV = 1024 * 1024; const int GB_DIV = 1024 * 1024 * 1024; while (true) { double cpu = sys.CpuLoad; double memory = (sys.PhysicalMemory - sys.MemoryAvailable) * 1.0 / (sys.PhysicalMemory * 1.0) * 100; this.CpuProgress = Math.Round(cpu); this.MemoryProgress = Math.Round(memory); if (IsWriteLog) { DateTime localTime = DateTime.Now; DateTime.TryParse(StartTime, out DateTime starTime); DateTime.TryParse(EndTime, out DateTime endTime); if (localTime > starTime && localTime < endTime) { WriteLog(new ProcessInfo { CpuRatio = this.CpuProgress.ToString(), MemeryRatio = this.MemoryProgress.ToString(), LocalTime = localTime.ToString(), ProcessName = "total" }); } } if (cpu > CpuLimitValue || memory > MemoryLimitValue) { AnyHelper.Alarm(); App.Current.Dispatcher.BeginInvoke((Action) delegate() { this.ShowNotification("警告", "请注意,CPU或者内存过载"); }); } Thread.Sleep(interval); } } catch (Exception ex) { LogHelper.WriteLog(ex.Message); } }); }
public void RealTimeHardDisk() { Task.Run(() => { try { while (true) { foreach (var item in objDriveInfo) { string diskName = item.Name; var hardDisk = HardDisks.Find(p => p.DiskName == diskName); if (hardDisk != null) { hardDisk.Available = AnyHelper.BytesToString(item.AvailableFreeSpace); if (item.AvailableFreeSpace < this.DiskLimitSize) { hardDisk.Tip = "磁盘空间不足"; AnyHelper.Alarm(); App.Current.Dispatcher.BeginInvoke((Action) delegate() { this.ShowNotification("警告", "请注意,磁盘控件不足,请及时清理"); }); } else { hardDisk.Tip = "正常"; } hardDisk.UsedRate = hardDisk.UsedRate = 100 - (int)(item.AvailableFreeSpace / (item.TotalSize * 1.00) * 100); } Thread.Sleep(2000); } } } catch (Exception ex) { LogHelper.WriteLog(ex.Message); } }); }