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 StartMonitoring() { objStopflag = false; Task.Run(() => { //PerformanceCounter curpcp = new PerformanceCounter("Process", "Working Set - Private", this.objProcess.ProcessName); //PerformanceCounter curpc = new PerformanceCounter("Process", "Working Set", this.objProcess.ProcessName); //PerformanceCounter curtime = new PerformanceCounter("Process", "% Processor Time", this.objProcess.ProcessName); //上次记录CPU的时间 TimeSpan prevCpuTime = TimeSpan.Zero; //Sleep的时间间隔 int interval = 1000; SystemInfo sys = new SystemInfo(); var info = new ProcessInfo(); info.ProcessId = objProcess.Id; info.ProcessName = objProcess.ProcessName; while (!objStopflag) { //当前时间 var curTime = objProcess.TotalProcessorTime; //间隔时间内的CPU运行时间除以逻辑CPU数量 double cpuValue = Math.Round((curTime - prevCpuTime).TotalMilliseconds / interval / Environment.ProcessorCount * 100, 2); double memValue = Math.Round((objProcess.WorkingSet64 * 1.00 / sys.PhysicalMemory * 1.00) * 100, 2); prevCpuTime = curTime; Console.WriteLine($"CPU使用率:{cpuValue}%"); //TimeSpan curCpuTime = this.objProcess.TotalProcessorTime; //double value = (curCpuTime - prevCpuTime).TotalMilliseconds / interval / Environment.ProcessorCount * 100; //prevCpuTime = curCpuTime; //long totalBytesOfMemoryUsed = objProcess.WorkingSet64; info.CpuRatio = $"{cpuValue}%"; info.MemeryRatio = $"{memValue}%"; info.LocalTime = DateTime.Now.ToLocalTime().ToLongTimeString(); AnyHelper.WriteCSV(this.objLogPath, info); //Console.WriteLine("{0}:{1} {2:N}KB CPU使用率:{3}", objProcess.ProcessName, "工作集(进程类)", objProcess.WorkingSet64 / 1024, value); //这个工作集只是在一开始初始化,后期不变 //Console.WriteLine("{0}:{1} {2:N}KB CPU使用率:{3}", objProcess.ProcessName, "工作集 ", curpc.NextValue() / 1024, value); //这个工作集是动态更新的 //第二种计算CPU使用率的方法 //Console.WriteLine("{0}:{1} {2:N}KB CPU使用率:{3}%", objProcess.ProcessName, "私有工作集 ", curpcp.NextValue() / 1024, curtime.NextValue() / Environment.ProcessorCount); Thread.Sleep(interval); } }); }
private void WriteLog(ProcessInfo info) { string dirPath = $"{logDir}\\log\\{DateTime.Today.ToString("yyyyMMdd")}"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string totalPath = dirPath + "\\" + "total.csv"; if (!File.Exists(totalPath)) { File.WriteAllLines(totalPath, new string[] { "进程名, 进程ID, 内存使用率, CPU使用率, 本地时间" }, Encoding.UTF8); } AnyHelper.WriteCSV(totalPath, info); }
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); } }); }
private void InitDrivers() { try { objDriveInfo = DriveInfo.GetDrives(); foreach (var item in objDriveInfo) { var hardDisk = new HardDisk(); hardDisk.DiskName = item.Name; hardDisk.Available = AnyHelper.BytesToString(item.AvailableFreeSpace); hardDisk.Total = AnyHelper.BytesToString(item.TotalSize); hardDisk.UsedRate = 100 - (int)(item.AvailableFreeSpace / (item.TotalSize * 1.00) * 100); hardDisk.Tip = "正常"; HardDisks.Add(hardDisk); } } catch (Exception ex) { LogHelper.WriteLog(ex.Message); } }
private bool IsValidTypeForAny(string parentType, string specializationType) { if (StringUtils.IsBlank(specializationType)) { return(false); } bool valid = AnyHelper.IsValidTypeForAny(parentType, specializationType); if (!valid) { // unqualify only the inner types string innerUnqualified = Hl7DataTypeName.UnqualifyInnerTypes(specializationType); valid = AnyHelper.IsValidTypeForAny(parentType, innerUnqualified); } if (!valid) { // unqualify both outer and inner types) string bothUnqualified = Hl7DataTypeName.Unqualify(specializationType); valid = AnyHelper.IsValidTypeForAny(parentType, bothUnqualified); } return(valid); }
public override string Format(FormatContext formatContext, BareANY hl7Value, int indentLevel) { if (hl7Value == null) { return(string.Empty); } string specializationType = hl7Value.DataType.Type; StandardDataType specializationTypeAsEnum = StandardDataType.GetByTypeName(specializationType); if (specializationTypeAsEnum != null && StandardDataType.ANY.Equals(specializationTypeAsEnum.RootDataType)) { // specializationType has been determined to be an ANY variant; this (most likely) means specializationType has not been specified, so don't do any special processing return(base.Format(formatContext, hl7Value, indentLevel)); } else { string mappedSpecializationType = this.polymorphismHandler.MapCdaR1Type(hl7Value.DataType, formatContext.IsCda()); PropertyFormatter formatter = FormatterRegistry.GetInstance().Get(mappedSpecializationType); string parentType = formatContext.Type; if (formatter == null || !AnyHelper.IsValidTypeForAny(parentType, specializationType)) { string errorText = "Cannot support properties of type " + specializationType + " for " + parentType + ". Please specify a specializationType applicable for " + parentType + " in the appropriate message bean."; throw new ModelToXmlTransformationException(errorText); } else { // pass processing off to the formatter applicable for the given specializationType StandardDataType type = hl7Value.DataType; return(formatter.Format(new Ca.Infoway.Messagebuilder.Marshalling.HL7.Formatter.FormatContextImpl(formatContext.GetModelToXmlResult (), formatContext.GetPropertyPath(), formatContext.GetElementName(), mappedSpecializationType, type.Coded ? "Code" : formatContext .GetDomainType(), formatContext.GetConformanceLevel(), formatContext.GetCardinality(), true, formatContext.GetVersion(), formatContext.GetDateTimeZone(), formatContext.GetDateTimeTimeZone(), null, formatContext.GetConstraints(), formatContext .IsCda()), hl7Value, indentLevel)); } } }