PROBLEMA [C#] Come prendere il reale utilizzo di CPU/RAM di un processo corrente ?

OttoOtti

Nuovo Utente
83
7
Salve ragazzi sto provando a inserire dentro una barra 4 dati : utilizzo corrente di un processo la quantità CPU,RAM,DISCO,RETE utilizzata .. ma aprendo il task-manager leggo percentuali ben diverse ... sto utilizzando using System.Diagnostics; ma sembra dare dati completamente a caso .. qualcuno cosce la soluzione a questo problema ? ho fatto molti test come potete vedere dal codice

asdasdasasdasd.png
C#:
using GUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows;
using System.Threading;

namespace Finestra
{
    public partial class Form1 : Form
    {

        bool meno_bool = false;
        Point size_point = new Point();

        //PerformanceCounter myAppCPU = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName, true);

        static readonly PerformanceCounter counter_cpu = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName, true);
        static readonly PerformanceCounter counter_ram = new PerformanceCounter("Process", "Working Set", Process.GetCurrentProcess().ProcessName);
        static readonly PerformanceCounter counter_disk = new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");
        //static readonly PerformanceCounter counter_disk = new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");

        int secondi = 0,minuti = 0,ore = 0,giorni = 0;
        // TEST
     

        private void Set_Finestra(int page)
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.MinimumSize = new System.Drawing.Size(200, 300);
            label1.Text = Convert.ToString(this.Size.Width) + "X" + Convert.ToString(this.Size.Height);

            timer1.Start();

            if(page > 0)
            {
                this.Location = new Point(this.Location.X,page);
            }
         
        }    

        private void timer1_Tick(object sender, EventArgs e)
        {
            secondi += 1;
            label3.Text = secondi.ToString();
            if (secondi == 60)
            {
                secondi = 0;
                minuti += 1;
            }else if(minuti == 60)
            {
                minuti = 0;
                ore += 1;
            }else if (ore == 24)
            {
                ore = 0;
                giorni += 1;
            }

            label3.Text = giorni.ToString() + " d : " + ore.ToString() + " h : " + minuti.ToString() + " m : " + secondi.ToString() + " s";

            // TEST
            System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
            long ram = p.WorkingSet64;
         
            label4.Text = Convert.ToInt32(counter_cpu.NextValue()).ToString() + "%";
            //label5.Text = Convert.ToInt32(counter_ram.NextValue() / 1024 / 1024).ToString() + " MB";
            //label5.Text = Convert.ToInt32(counter_ram.NextValue()).ToString();
            label5.Text = $": {ram / 1024 / 1024} MB";
            label7.Text = Convert.ToInt32(counter_disk.NextValue()).ToString();

        }

        // BARRA
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            Utility.Move(sender, e, Handle);
        }

        // BOTTONE SIZE
        private void button5_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Size = new Size(this.Size.Width + (e.X - size_point.X), this.Size.Height + (e.Y - size_point.Y));
                label1.Text = Convert.ToString(this.Size.Width) + "X" + Convert.ToString(this.Size.Height);
            }
        }

        // BOTTONE SIZE
        private void button5_MouseDown(object sender, MouseEventArgs e)
        {
            size_point = new Point(e.X, e.Y);
        }

        // BOTTONE X
        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        // BOTTONE +
        private void button1_Click(object sender, EventArgs e)
        {

            if (this.WindowState == System.Windows.Forms.FormWindowState.Maximized)
            {
                this.WindowState = System.Windows.Forms.FormWindowState.Normal;
                label1.Text = Convert.ToString(this.Size.Width) + "X" + Convert.ToString(this.Size.Height);
            }
            else
            {
                this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
                label1.Text = Convert.ToString(this.Size.Width) + "X" + Convert.ToString(this.Size.Height);
            }

        }

        // BOTTONE -
        private void button3_Click(object sender, EventArgs e)
        {
            if (meno_bool == false)
            {
                panel4.Visible = false;
                panel2.Visible = false;
                panel3.Location = new Point(0, 32);
                panel3.Size = new Size(this.Size.Width, this.Size.Height-32);
                meno_bool = true;
            }
            else
            {
                panel4.Visible = true;
                panel2.Visible = true;
                panel3.Location = new Point(0, 72);
                panel3.Size = new Size(this.Size.Width, this.Size.Height-94);
                meno_bool = false;
            }
        }

        // BOTTONE TRIANGOLO
        private void button2_Click(object sender, EventArgs e)
        {
         
        }

        private void Bottom_Bar(string stato)
        {
            if(stato == "pronto")
            {
                panel2.BackColor = Color.FromName("MenuHighlight");
                label2.Text = "Pronto";
            }
            else if(stato == "errore")
            {
                panel2.BackColor = Color.FromName("Firebrick");
                label2.Text = "Errore";
            }
            else if(stato == "esecuzione")
            {
                panel2.BackColor = Color.FromName("Orange");
                label2.Text = "Esecuzione";
            }
            else if (stato == "attesa")
            {
                panel2.BackColor = Color.FromName("Gold");
                label2.Text = "Attesa";
            }
        }

        // END CLASS
    }
}
 
Ultima modifica da un moderatore:

pabloski

Utente Èlite
2,868
916
Ma il task manager non è affatto preciso. Lo strumento più preciso è appunto PerformanceCounter. Se usi perfmon.exe e setti un contatore, oppure se usi process explorer, vedrai che i numeri sono gli stessi di PerformanceCounter.
 
  • Mi piace
Reazioni: Andretti60

Entra

oppure Accedi utilizzando
Discord Ufficiale Entra ora!