Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Get-Process | Sort-Object CPU -desc | Select-Object -first 3 | Format-Table | CPU,ProcessName -hidetableheader
- 16.8041632 System
- 12.568072 csrss
- 11.9872368 powershell
- using System.Diagnostics;
- float GetAverageCPULoad(int procID, DateTme from, DateTime, to)
- {
- // For the current process
- //Process proc = Process.GetCurrentProcess();
- // Or for any other process given its id
- Process proc = Process.GetProcessById(procID);
- System.TimeSpan lifeInterval = (to - from);
- // Get the CPU use
- float CPULoad = (proc.TotalProcessorTime.TotalMilliseconds /
- lifeInterval.TotalMilliseconds) * 100;
- // You need to take the number of present cores into account
- return CPULoad / System.Environment.ProcessorCount;
- }
- class ProcLoad
- {
- // Last time you checked for a process
- public Dictionary<int, DateTime> lastCheckedDict = new Dictionary<int, DateTime>();
- public float GetCPULoad(int procID)
- {
- if (lastCheckedDict.ContainsKey(procID))
- {
- DateTime last = lastCheckedDict[procID];
- lastCheckedDict[procID] = DateTime.Now;
- return GetAverageCPULoad(procID, last, lastCheckedDict[procID]);
- }
- else
- {
- lastCheckedDict.Add(procID, DateTime.Now);
- return 0;
- }
- }
- }
- Process.TotalProcessorTime
Add Comment
Please, Sign In to add comment