Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我关闭表单/程序时,以及每X分钟,我如何找到最高、最低和平均数字?_C# - Fatal编程技术网

C# 当我关闭表单/程序时,以及每X分钟,我如何找到最高、最低和平均数字?

C# 当我关闭表单/程序时,以及每X分钟,我如何找到最高、最低和平均数字?,c#,C#,现在我将通过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.Diagnosti

现在我将通过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.076494,最小值0,平均值2.947297

但是现在我先得到值,然后是文本,值和文本之间没有空格

我现在唯一的问题是最后一行string.Format

大约每5分钟我就要通过一次,因为现在我不会这样做。

1。)您不需要保存所有中间值,所以列表太多了。
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();
        }
    }

    // ...
}
公共部分类表单1:表单
{
// ...
专用浮点数;
专用浮点maxCpu;
专用浮点数;
私人室内滴答声;
//也可以在构造函数中调用它
私有无效重置()
{
this.minCpu=Single.PositiveInfinity;
this.maxCpu=Single.NegativeInfinity;
这个.sumcu=0;
此参数为0;
}
// ...
私有无效计时器1_刻度(对象发送方,事件参数e)
{
// ...
//更新
如果(this.minCpu>cpuUsage)this.minCpu=cpuUsage;
如果(this.maxCpu=5*60)/=5分钟,因为每秒调用一次
{
float avgCpu=this.sumCpu/this.ticks;
//将this.minCpu、this.maxCpu和avgCpu写入日志
重置();
}
}
// ...
}

不,你没有。请参见上文。

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

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

老实说,我不知道你怎么能写下其余的内容,却无法理解。我建议你睡一觉。

你的编辑是一个全新的问题。。。我不得不投了反对票。我同意你的看法,因为这是一个新问题。但我还是回答了。
 string t = string.Format("Maximum --- {0} , Minimum --- {1} , Average --- {2}", Maximum, Minimum, Average);