现在我将通过 X 分钟,我暂时不会这样做。其余的是我现在使用的代码。
我试着这样做:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using DannyGeneral;
namespace CpuUsage
{
public partial class Form1 : Form
{
private PerformanceCounter theCPUCounter;
private PerformanceCounter theMemCounter;
private PerformanceCounter specProcessCPUCounter;
private float cpuUsage;
private float memUsage;
private string processname;
private List<float> Values;
public Form1()
{
InitializeComponent();
Values = new List<float>();
theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
theMemCounter = new PerformanceCounter("Memory", "Available MBytes");
specProcessCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
memUsage = theMemCounter.NextValue();
label1.Text = memUsage.ToString();
Logger.Write("Memory Usage " + memUsage.ToString());
cpuUsage = this.theCPUCounter.NextValue();
label2.Text = cpuUsage.ToString();
Logger.Write("Cpu Usage " + this.cpuUsage.ToString());
Values.Add(cpuUsage);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
float Maximum = Values.Max();
float Minimum = Values.Min();
float Average = Values.Average();
string t = string.Format("{0}Maximum,{1}Minimum,{2}Average", Maximum, Minimum, Average);
Logger.Write(t);
}
}
}
问题是记录器文本文件中的结果是这样的:
8/29/2012--1:57 AM ==> Memory Usage 2683
8/29/2012--1:57 AM ==> Cpu Usage 0
8/29/2012--1:57 AM ==> Memory Usage 2681
8/29/2012--1:57 AM ==> Cpu Usage 5.951914
8/29/2012--1:57 AM ==> Memory Usage 2675
8/29/2012--1:57 AM ==> Cpu Usage 1.15339
8/29/2012--1:57 AM ==> Memory Usage 2674
8/29/2012--1:57 AM ==> Cpu Usage 4.230328
8/29/2012--1:57 AM ==> Memory Usage 2674
8/29/2012--1:57 AM ==> Cpu Usage 1.345688
8/29/2012--1:57 AM ==> Memory Usage 2677
8/29/2012--1:57 AM ==> Cpu Usage 4.422635
8/29/2012--1:57 AM ==> Memory Usage 2676
8/29/2012--1:57 AM ==> Cpu Usage 0.768766
8/29/2012--1:57 AM ==> Memory Usage 2676
8/29/2012--1:57 AM ==> Cpu Usage 0.5764585
8/29/2012--1:57 AM ==> Memory Usage 2676
8/29/2012--1:57 AM ==> Cpu Usage 8.076494
8/29/2012--1:58 AM ==> 8.076494Maximum,0Minimum,2.947297Average
格式的最后一行不像我想要的那样。我希望它是这样的:最大值 --- 8.076494 ,最小值 --- 0 ,平均值 --- 2.947297
但是现在我首先得到值然后是文本,并且值和每个文本之间没有空格。
我现在唯一的问题是最后一行 string.Format
大约每 5 分钟我会通过它,现在不会这样做。