簡體   English   中英

如何從進程開始和結束之間的時間獲取數據?

[英]How can i get the data from the time between a process started and ended?

我有這個代碼:

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 List<float> processValues;
        private bool alreadyRun;
        private DateTime dt;
        private DateTime dt1;
        private PerformanceCounter theCPUCounter;
        private PerformanceCounter theMemCounter;
        private PerformanceCounter specProcessCPUCounter;
        private float cpuUsage;
        private float memUsage;
        private List<float> Values;

        public Form1()
        {
            InitializeComponent();

            processValues = new List<float>();
            alreadyRun = false;
            dt = DateTime.Now;


            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);
            isProcessRunning();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            dt1 = DateTime.Now;
            float Maximum = Values.Max();
            float Minimum = Values.Min();
            float Average = Values.Average();
            string t = string.Format("Maximum --- {0} , Minimum --- {1} , Average --- {2}", Maximum, Minimum, Average);
            Logger.Write(t);



            TimeSpan ts = (dt1 - dt);
            string time = ts.ToString(@"hh\:mm\:ss");
            Logger.Write("Time The Program Was Running ---   " + time);


        }

        private void isProcessRunning()
        {

                Process[] proclist = Process.GetProcessesByName("BFBC2Game");

                    if (proclist.Length > 0)
                    {
                        if (alreadyRun == false)
                        {
                            Logger.Write("Bfbc2 Started");
                            alreadyRun = true;
                        }
                    }
                    else
                    {
                        if (alreadyRun == true)
                        {
                            Logger.Write("Bfbc2 Exited");
                            alreadyRun = false;
                        }
                    }

        }

    }
}

我剛剛添加了一個新列表:processValues。 我想要做的是,當我退出進程時,例如在這種情況下:BFBC2Game 它將從進程開始到結束時為我寫入 Max() Min() 和 Average() 記錄器。

問題是該過程可能會在程序運行幾分鍾后開始並已記錄一些信息。 我只想在過程開始和結束的時間之間獲得最大最小值和平均值。

所以當我關閉表單時,程序退出記錄器文本文件中的程序,我現在看到最后:

8/31/2012--12:18 AM ==> Maximum --- 42.05113 , Minimum --- 0 , Average --- 13.19243
8/31/2012--12:18 AM ==> Time The Program Was Running ---   00:00:49

當進程開始和退出時,我看到中間的某個地方:

8/31/2012--12:17 AM ==> Memory Usage   4435
8/31/2012--12:17 AM ==> Cpu Usage   5.57648
8/31/2012--12:17 AM ==> Bfbc2 Started
8/31/2012--12:17 AM ==> Memory Usage   4412
8/31/2012--12:17 AM ==> Cpu Usage   15.38422
8/31/2012--12:17 AM ==> Bfbc2 Exited
8/31/2012--12:18 AM ==> Memory Usage   4432
8/31/2012--12:18 AM ==> Cpu Usage   13.26883

現在我希望當我退出應用程序時,它會在文件末尾寫下這樣的內容:

8/31/2012--12:18 AM ==> Bfbc2 Started at 8/31/2012--12:17 AM
8/31/2012--12:19 AM ==> Maximum --- 42.05113 , Minimum --- 0 , Average --- 13.19243
8/31/2012--12:19 AM ==> Bfbc2 Exited at 8/31/2012--12:17 AM

所以我會知道在進程開始和結束時查看文件的末尾,以及進程運行時間的最小值和平均值混合值是多少。

您可以使用 System.Diagnostics.Stopwatch 類。 這是一個示例代碼。 這是指向相應 Microsoft 文檔的鏈接

using System;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        int[] array = new int[] { 4, 2, 5, 1, 3 };
        Stopwatch clock = Stopwatch.StartNew();
        SortRecursivly(array);
        clock.Stop();
        Console.WriteLine($"Array sorted in {clock.ElapsedMilliseconds}ms");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM