Skip to main content

ProcessMetrics

A class in Root.Township, the global namespace.

Old CPU and RAM sampler for one Process, marked obsolete in favour of the new analytics system. Creating one starts a background thread that samples CPU once a second until Stop.

Constructors​

MemberWhat it is
ProcessMetrics(Process process)Creates a ProcessMetrics from process.

Fields​

7 private and internal fields
MemberWhat it is
private static TimeSpan initialProcessorTimeProcessor time of the process when the sampler was made. Static, so a second instance overwrites it.
private bool isRunningKeeps the sampler thread looping. Stop clears it.
private long lastMonitorTimeStopwatch time in milliseconds of the previous sample.
private static TimeSpan oldCPUTimeProcessor time used at the previous sample. Static, shared by every instance.
private Process processThe process being measured.
private int processorCountNumber of logical cores, used to scale CPU to a 0 to 1 range.
private Stopwatch totalDurationStopwatch started when the sampler was made; gives the wall time between samples.

Properties​

MemberWhat it is
double CPU { get; private set; }CPU use over the last sample, as a fraction of all processor cores (0 to 1). Updated once a second by the sampler thread.
long RAM { get; }Current working set of the process in bytes, read fresh from Windows each time you get it.

Methods​

MemberWhat it is
void Stop()Ends the sampler thread after its current one-second sleep.
3 private and internal methods
MemberWhat it is
private static bool GetProcessMemoryInfo(IntPtr processHandle, out ProcessMetrics.MemoryCounters info, int Size)Native psapi.dll call that fills a MemoryCounters for a process handle. Windows only; RAM uses it.
private void UpdateMetric()Works out CPU from the processor time used since the last sample. Called by UpdateMetricLoop.
private void UpdateMetricLoop()Body of the background thread: calls UpdateMetric every second while isRunning is true.

Used in​

Members of other types that take, return or hold a ProcessMetrics: