/// <summary> /// 写出运行时状态信息 /// </summary> private void WriteStatus(StatusLogModel status) { try { if (status != null) { //处理比率 status.Long = status.Long / 1000; status.AFK = status.AFK / 1000; if (status.AFK > status.Long) { status.AFK = status.Long; } //设置日志目录和日志文件 string path = DirTool.Combine(LogPath, "status"); string file = DirTool.Combine(path, DateTime.Now.ToString("yyyy-MM-dd") + ".txt"); //创建日志目录 DirTool.Create(path); //写出日志 TxtTool.Append(file, status.ToString()); } Cleaner(); } catch { } }
public bool Start() { //如果任务停止运行,则重新创建Token,并释放上次任务 if (Listener != null && Listener.Status != TaskStatus.Running) { try { CancelToken = new CancellationTokenSource(); Listener.Dispose(); } catch { } } //如果任务没取消,并且没有运行任务,则运行任务 if (!CancelToken.IsCancellationRequested && (Listener == null || Listener.Status != TaskStatus.Running)) { Listener = Task.Factory.StartNew(() => { try { WriteConfig(); } catch { } try { long afk = WindowsAPI.GetLastInputTime(); //空闲时间缓存 TimeSpan pin = TimeSpan.Zero; //程序运行时间戳 StatusLogModel status = null; //运行状态信息模型 DateTime recCycTime = DateTime.Now; int count = 1; while (!CancelToken.IsCancellationRequested) { pin = AppProcess.TotalProcessorTime; //程序运行时间戳 Thread.Sleep(Interval); //等待间隔时间 //每秒钟都会执行的操作 DateTime now = DateTime.Now; int time = (int)(now - recCycTime).TotalMilliseconds; CollectStatus(ref status, count, time, ref afk, pin);//收集数据 if (!(recCycTime.Year == now.Year && recCycTime.Month == now.Month && recCycTime.Day == now.Day && recCycTime.Hour == now.Hour && recCycTime.Minute == now.Minute)) { WriteStatus(status); //写出数据 count = 1; //重置运行时间 status = null; //重置数据 recCycTime = DateTime.Now; } count++; } } catch { } }, CancelToken.Token); return(true); } return(false); }
public StatusLogModel FromString(string s) { StatusLogModel model = new StatusLogModel(); string[] elements = s.Split('|'); if (ListTool.HasElements(elements)) { try { if (elements.Length > 0) { model.Time = DateTime.Parse(elements[0]); } } catch { } try { if (elements.Length > 1) { model.Long = int.Parse(elements[1]); } } catch { } try { if (elements.Length > 2) { model.AFK = long.Parse(elements[2]); } } catch { } try { if (elements.Length > 3) { model.CpuPer = int.Parse(elements[3]); } } catch { } try { if (elements.Length > 5) { model.RamFree = long.Parse(elements[5]); } } catch { } try { if (elements.Length > 7) { model.SysDriveFree = long.Parse(elements[7]); } } catch { } try { if (elements.Length > 8) { model.AppCpuPer = int.Parse(elements[8]); } } catch { } try { if (elements.Length > 9) { model.AppRamUsed = long.Parse(elements[9]); } } catch { } } return(model); }
public bool Start() { //如果任务停止运行,则重新创建Token,并释放上次任务 if (Listener != null && Listener.Status != TaskStatus.Running) { try { CancelToken = new CancellationTokenSource(); Listener.Dispose(); } catch { } } //如果任务没取消,并且没有运行任务,则运行任务 if (!CancelToken.IsCancellationRequested && (Listener == null || Listener.Status != TaskStatus.Running)) { Listener = Task.Factory.StartNew(() => { try { WriteConfig(); int runtime = 0; //运行时间(毫秒) long afk = WindowsAPI.GetLastInputTime(); //空闲时间缓存 TimeSpan pin = TimeSpan.Zero; //程序运行时间戳 StatusLogModel status = null; //运行状态信息模型 while (!CancelToken.IsCancellationRequested) { pin = AppProcess.TotalProcessorTime; //程序运行时间戳 runtime += Interval; //增加运行时间 Thread.Sleep(Interval); //等待间隔时间 //每秒钟都会执行的操作 CollectStatus(ref status, runtime, afk, pin); //收集数据 afk = WindowsAPI.GetLastInputTime(); //空闲时间缓存 //每分钟进行汇总输出 if (runtime >= WriteInterval) { WriteStatus(status); //写出数据 runtime = 0; //重置运行时间 status = null; //重置数据 } } } catch { } }, CancelToken.Token); return(true); } return(false); }
/// <summary> /// 收集数据 /// </summary> /// <returns></returns> private bool CollectStatus(ref StatusLogModel status, int count, int runtime, ref long afk, TimeSpan pin) { try { if (status == null) { status = new StatusLogModel() { Time = DateTime.Now } } ; //固定值数据 status.Long = runtime;//运行时长 //累计值数据 long nowAfk = WindowsAPI.GetLastInputTime(); long afktemp = nowAfk - afk; if (afktemp > 0) { status.AFK = status.AFK + afktemp; } //Console.WriteLine($"nowAfk: {nowAfk}, afk: {afk}, afktemp: {afktemp}, statusAFK: {status.AFK}"); afk = nowAfk; //计算平均值数据 int cpu = 0; try { cpu = (int)ComputerProcessor.NextValue(); } catch { }//CPU占用 long ram = (long)ComputerInfoTool.AvailablePhysicalMemory();//系统可用内存 int appcpu = (int)AppInfoTool.CalcCpuRate(AppProcess, pin, Interval);//程序CPU占用 long appram = AppInfoTool.RAM(); //程序内存占用 long sysdisk = ComputerInfoTool.GetSystemDriveAvailableSize(); //系统盘可用空间 status.CpuPer = ((count - 1) * status.CpuPer + cpu) / count; //CPU占用 status.RamFree = ((count - 1) * status.RamFree + ram) / count; //系统可用内存 status.AppCpuPer = ((count - 1) * status.AppCpuPer + appcpu) / count; //程序CPU占用 status.AppRamUsed = ((count - 1) * status.AppRamUsed + appram) / count; //程序内存占用 status.SysDriveFree = ((count - 1) * status.SysDriveFree + sysdisk) / count; //系统盘可用空间 return(true); } catch { return(false); } }