public void Run() { _process.Refresh(); ProcessPhysicalMemoryGauge.SetValue(Environment.WorkingSet); ProcessTotalProcessorTime.SetValue(_process.TotalProcessorTime.Ticks); ProcessTotalProcessorTime.SetValue(_process.UserProcessorTime.Ticks); _newTime = _process.TotalProcessorTime.TotalMilliseconds; _newStamp = DateTime.Now; // calculates CPU usage since last measurement _change = _newTime - _oldTime; // calculates time between CPU measurements _period = _newStamp.Subtract(_oldStamp).TotalMilliseconds; _oldTime = _newTime; _oldStamp = _newStamp; double use = (_change / (_period * _processorTotal) * 100.0); // if sampling error causes CPU to read over 100, set to 100 if (use > 100.0) { use = 100.0; } CpuUsage.SetValue(use); ThreadPool.GetAvailableThreads(out int threads, out int complPorts); Threads.SetValue(_maxThreads - threads); CompletionPorts.SetValue(_maxCompletionPorts - complPorts); }
/// <summary> /// ExecuteAsync /// </summary> /// <param name="context"></param> /// <param name="cancellationToken"></param> /// <returns></returns> protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken) { // Inputs var process = Process.Get(context); var ret = MonitorProcess.GetMonitoredValue(process); ulong[] eachCpu = { ret[MonitorItem.SystemEachCpu00to07], ret[MonitorItem.SystemEachCpu08to15], ret[MonitorItem.SystemEachCpu16to23], ret[MonitorItem.SystemEachCpu24to31] }; int cpuCount = Environment.ProcessorCount; if (cpuCount > MonitorProcess.MAX_CPU_COUNT) { cpuCount = MonitorProcess.MAX_CPU_COUNT; } float[] eachCpuArray = new float[cpuCount]; for (int i = 0; i < cpuCount; i++) { ulong cpuValue = eachCpu[i >> 2]; eachCpuArray[i] = (float)(cpuValue & 0xFF); eachCpu[i >> 2] = eachCpu[i >> 2] >> 8; } // Outputs return((ctx) => { RawValue.Set(ctx, ret); SystemTotalCpu.Set(ctx, (float)ret[MonitorItem.SystemTotalCpu] / 100F); SystemTotalCpuMA.Set(ctx, (float)ret[MonitorItem.SystemTotalCpuMA] / 100F); SystemEachCpu.Set(ctx, eachCpuArray); SystemTotalPhysicalMemory.Set(ctx, ret[MonitorItem.SystemTotalPhysicalMemory]); SystemAvailablePhisycalMemory.Set(ctx, ret[MonitorItem.SystemAvailablePhisycalMemory]); SystemTotalVirtualMemory.Set(ctx, ret[MonitorItem.SystemTotalVirtualMemory]); SystemAvilableVirtualMemory.Set(ctx, ret[MonitorItem.SystemAvilableVirtualMemory]); ProcessWorkingSet.Set(ctx, ret[MonitorItem.ProcessWorkingSet]); ProcessWorkingSetMA.Set(ctx, ret[MonitorItem.ProcessWorkingSetMA]); ProcessPrivateMemorySize.Set(ctx, ret[MonitorItem.ProcessPrivateMemorySize]); ProcessPrivateMemorySizeMA.Set(ctx, ret[MonitorItem.ProcessPrivateMemorySizeMA]); ProcessTotalManagedMemory.Set(ctx, ret[MonitorItem.ProcessTotalManagedMemory]); ProcessTotalProcessorTime.Set(ctx, ret[MonitorItem.ProcessTotalProcessorTime]); ProcessTotalProcessorTimeDelta.Set(ctx, ret[MonitorItem.ProcessTotalProcessorTimeDelta]); ProcessResponding.Set(ctx, ret[MonitorItem.ProcessResponding] != 0 ? true : false); ProcessRespondingCount.Set(ctx, (int)ret[MonitorItem.ProcessRespondingCount]); }); }