This is a migrated thread and some comments may be shown as answers.

Display CPU usage and Memory Usage in GridView

5 Answers 414 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Iron
Iron
Brian asked on 24 Sep 2014, 02:56 PM
I am experimenting with an application that will give me CPU Usage, Memory Usage, and I want to expand it from there. Right now it goes to two Text Boxes. I combined it into the first text box 

How do I display each value in two GridView Columns?

Here is what I have now;

using System;
using System.Diagnostics;
using System.Data;
using System.Windows.Forms;
 
namespace MemOV
{
    public partial class FrmMain : Form
    {
        private PerformanceCounter cpuCounter;
        private PerformanceCounter ramCounter;
 
        public FrmMain()
        {
            InitializeComponent();
            InitializeCpuCounter();
            InitializeRamCounter();
            updateTimer.Start();
        }
 
        private void UpdateTimerTick(object sender, EventArgs e)
        {
            this.textBox1.Text = "CPU Usage: " + Convert.ToInt32(cpuCounter.NextValue()).ToString() + "%" + "  | " + "Memory Usage: " + Convert.ToInt32(ramCounter.NextValue()).ToString() + "Mb";
            this.textBox2.Text = Convert.ToInt32(ramCounter.NextValue()).ToString() + "Mb";
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void InitializeCpuCounter()
        {
            cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
        }
 
        private void InitializeRamCounter()
        {
            ramCounter = new PerformanceCounter("Memory", "Available MBytes", true);
        }
    }
}


5 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 25 Sep 2014, 12:55 PM
Hi Brian,

Thank you for contacting us.

First you can add the two columns to the grid:
public Form1()
{
    InitializeComponent();
 
    GridViewTextBoxColumn cpuColumn= new GridViewTextBoxColumn("CPU");
    cpuColumn.Width = 250;
    cpuColumn.ReadOnly = true;
    radGridView1.Columns.Add(cpuColumn);
 
    GridViewTextBoxColumn ramColumn = new GridViewTextBoxColumn("RAM");
    ramColumn.Width = 70;
    ramColumn.ReadOnly = true;
    radGridView1.Columns.Add(ramColumn);
 
    radGridView1.AllowAddNewRow = false;
    radGridView1.Rows.AddNew();
}

Then you can directly set the column values in the tick event:
private void UpdateTimerTick(object sender, EventArgs e)
{
    radGridView1.Rows[0].Cells["CPU"].Value = "CPU Usage: " + Convert.ToInt32(cpuCounter.NextValue()).ToString() + "%" + "  | " + "Memory Usage: " + Convert.ToInt32(ramCounter.NextValue()).ToString() + "Mb";
 
    radGridView1.Rows[0].Cells["RAM"].Value = Convert.ToInt32(ramCounter.NextValue()).ToString() + "Mb";
}

Please let me know if there is something else I can help you with. 
 
Regards,
Dimitar
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Brian
Top achievements
Rank 1
Iron
Iron
answered on 25 Sep 2014, 01:39 PM
Dimitar, thank you. That is exactly what I am looking for. I have a follow on question. If I want to add the Private working memory of a process, say Notepad.exe for example how would I add that in?

Thanks
0
Accepted
Dimitar
Telerik team
answered on 25 Sep 2014, 03:23 PM
Hello Brian,

Thank you for writing back.

You can add new row and set the value of the cell just like before:
Process[] currentProcesess;
  
public Form1()
{
    InitializeComponent();
    
    currentProcesess = System.Diagnostics.Process.GetProcessesByName("notepad");
      
    foreach (Process item in currentProcesess)
    {
        radGridView1.Rows.Add(item.ProcessName + " " + item.WorkingSet64);
    }
}
  
private void UpdateTimerTick(object sender, EventArgs e)
{
    for (int i = 0; i < currentProcesess.Length; i++)
    {
        radGridView1.Rows[i + 1].Cells["CPU"].Value = currentProcesess[i].ProcessName + " " + currentProcesess[i].WorkingSet64; //assumes that you already have one row for the cpu usage
    }
}

I hope this helps.

Regards,
Dimitar
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Brian
Top achievements
Rank 1
Iron
Iron
answered on 25 Sep 2014, 07:10 PM
Thanks Dimitar, I see that it adds it as a separate row item. I added another column.

This is what I have now; I left the TextBoxes that I first used just so I could validate the correct data made in in the field, so that can be removed.

I will take a look at the videos you referenced. Thanks!
​
using System;
using System.Diagnostics;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        private PerformanceCounter cpuCounter;
        private PerformanceCounter ramCounter;
        Process[] currentProcesess;
        public Form1()
        {
            InitializeComponent();
 
            currentProcesess = System.Diagnostics.Process.GetProcessesByName("notepad");
            foreach (Process item in currentProcesess)
            {
                radGridView1.Rows.Add(item.ProcessName + " " + item.WorkingSet64);
            }
 
            GridViewTextBoxColumn cpuColumn = new GridViewTextBoxColumn("CPU");
            cpuColumn.Width = 250;
            cpuColumn.ReadOnly = true;
            radGridView1.Columns.Add(cpuColumn);
 
            GridViewTextBoxColumn ramColumn = new GridViewTextBoxColumn("RAM");
            ramColumn.Width = 70;
            ramColumn.ReadOnly = true;
            radGridView1.Columns.Add(ramColumn);
            radGridView1.AllowAddNewRow = false;
            radGridView1.Rows.AddNew();
 
            GridViewTextBoxColumn procColumn = new GridViewTextBoxColumn("PROC");
            procColumn.Width = 100;
            procColumn.ReadOnly = true;
            radGridView1.Columns.Add(procColumn);
            radGridView1.AllowAddNewRow = false;
            radGridView1.Rows.AddNew();
 
            InitializeCpuCounter();
            InitializeRamCounter();
            updateTimer.Start();
        }
 
        private void UpdateTimerTick(object sender, EventArgs e)
        {
            this.textBox1.Text = "CPU Usage: " + Convert.ToInt32(cpuCounter.NextValue()).ToString() + "%" + "  | " + "Memory Usage: " + Convert.ToInt32(ramCounter.NextValue()).ToString() + "Mb";
            this.textBox2.Text = Convert.ToInt32(ramCounter.NextValue()).ToString() + "Mb";
            radGridView1.Rows[0].Cells["CPU"].Value = "CPU Usage: " + Convert.ToInt32(cpuCounter.NextValue()).ToString() + "%" + "  | " + "Memory Usage: " + Convert.ToInt32(ramCounter.NextValue()).ToString() + "Mb";
            radGridView1.Rows[0].Cells["RAM"].Value = Convert.ToInt32(ramCounter.NextValue()).ToString() + "Mb";
            for (int i = 0; i < currentProcesess.Length; i++)
            {
                radGridView1.Rows[0].Cells["PROC"].Value = currentProcesess[i].ProcessName + " " + currentProcesess[i].WorkingSet64; //assumes that you already have one row for the cpu usage
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void InitializeCpuCounter()
        {
            cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
        }
 
        private void InitializeRamCounter()
        {
            ramCounter = new PerformanceCounter("Memory", "Available MBytes", true);
        }
    }
}
0
Accepted
Dimitar
Telerik team
answered on 26 Sep 2014, 12:12 PM
Hi Brian,

I am glad I could be of help. Let us know if you have any other questions.
 
Regards,
Dimitar
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Brian
Top achievements
Rank 1
Iron
Iron
Answers by
Dimitar
Telerik team
Brian
Top achievements
Rank 1
Iron
Iron
Share this question
or