-1

现在我将通过 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 分钟我会通过它,现在不会这样做。

4

2 回答 2

2

1.) 你不需要保存所有的中间值,所以 List 太多了。
2.) 是的,它会的。
3.)试试这个:

public partial class Form1 : Form
{
    // ...
    private float minCpu;
    private float maxCpu;
    private float sumCpu;
    private int ticks;

    // also call this in constructor
    private void reset()
    {
       this.minCpu = Single.PositiveInfinity;
       this.maxCpu = Single.NegativeInfinity;
       this.sumCpu = 0;
       this.ticks  = 0;
    }

    // ...
    private void timer1_Tick(object sender, EventArgs e)
    {
        // ...
        // update
        if (this.minCpu > cpuUsage) this.minCpu = cpuUsage;
        if (this.maxCpu < cpuUsage) this.maxCpu = cpuUsage;
        this.sumCpu += cpuUsage;
        this.ticks++;

        if (this.ticks >= 5 * 60) // = 5 min, since this is called every second
        {
            float avgCpu = this.sumCpu / this.ticks;
            // write this.minCpu, this.maxCpu, and avgCpu to Log
            reset();
        }
    }

    // ...
}

4.) 不,你没有。看上面。

于 2012-08-28T22:43:11.653 回答
0

唯一的问题是字符串格式???好的,你去:

 string t = string.Format("Maximum --- {0} , Minimum --- {1} , Average --- {2}", Maximum, Minimum, Average);

老实说,我不知道你怎么能写出剩下的却无法弄清楚。建议睡一觉。

于 2012-08-29T03:32:32.440 回答