public static PerfomanceInfoData GetPerformanceInfo() { PerfomanceInfoData data = new PerfomanceInfoData(); PsApiPerformanceInformation perfInfo = new PsApiPerformanceInformation(); if (GetPerformanceInfo(out perfInfo, Marshal.SizeOf(perfInfo))) { /// data in pages data.CommitTotalPages = perfInfo.CommitTotal.ToInt64(); data.CommitLimitPages = perfInfo.CommitLimit.ToInt64(); data.CommitPeakPages = perfInfo.CommitPeak.ToInt64(); /// data in bytes Int64 pageSize = perfInfo.PageSize.ToInt64(); data.PhysicalTotalBytes = perfInfo.PhysicalTotal.ToInt64() * pageSize; data.PhysicalAvailableBytes = perfInfo.PhysicalAvailable.ToInt64() * pageSize; data.SystemCacheBytes = perfInfo.SystemCache.ToInt64() * pageSize; data.KernelTotalBytes = perfInfo.KernelTotal.ToInt64() * pageSize; data.KernelPagedBytes = perfInfo.KernelPaged.ToInt64() * pageSize; data.KernelNonPagedBytes = perfInfo.KernelNonPaged.ToInt64() * pageSize; data.PageSizeBytes = pageSize; /// counters data.HandlesCount = perfInfo.HandlesCount; data.ProcessCount = perfInfo.ProcessCount; data.ThreadCount = perfInfo.ThreadCount; } return(data); }
/// <summary> /// Update performance stats on timer. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void meterTimer_Tick(object sender, EventArgs e) { try { string cpuPerc = Math.Round(cpuCounter.NextValue(), MidpointRounding.AwayFromZero).ToString(); perfData = PsApiWrapper.GetPerformanceInfo(); string memPerc = Math.Round(100 - (((decimal)perfData.PhysicalAvailableBytes / (decimal)perfData.PhysicalTotalBytes) * 100), MidpointRounding.AwayFromZero).ToString(); cpuTextBox.Text = cpuPerc; memTextBox.Text = memPerc; if (connectButton.Text == "Dis&connect") { string portData = "C" + cpuPerc + "\rM" + memPerc + "\r"; meterPort.Write(portData); } } catch (IOException) { //Serial failed, maybe device was unplugged? Disable serial and resume. //Disable timer (so we only see error once) meterTimer.Enabled = false; //Dispose serial DisposeSerial(); //Change form back connectButton.Text = "&Connect"; comPortUpDown.Enabled = true; MessageBox.Show("Communication with the device has been lost. Has it been unplugged?", "Serial I/O Error", MessageBoxButtons.OK, MessageBoxIcon.Error); //Now that serial is disconnected, it's safe to reenable timer. meterTimer.Enabled = true; } catch (Exception caught) { //Disable timer (so we only see error once) meterTimer.Enabled = false; WinFormHelper.DisplayErrorMessage("Update meters", caught); //Now that the timer is disabled, the program isn't very useful. Close. exitApp = true; this.Close(); } }