Solved

Computer CPU/Memory usage

Posted on 2010-08-29
2
1,078 Views
Last Modified: 2012-05-10
Hi,
is there a way, beside using WMI, to know the total Cpu % used and memory % used?

If not, do you have a code example using WMI?

I remember working with WMI in the past, but it was working on some computer and not working on other(returning empty information). Im not sure exactly what are the requirements for WMI to work on Windows XP and recent OS(vista, 7).

Thanks in advance!
0
Comment
Question by:Enill
2 Comments
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 500 total points
Comment Utility
Hi,

is there a way,  beside using WMI, to know the total Cpu % used and memory % used?

Check attach code ITEM-1.

Also visit links:

(1) http://www.geekpedia.com/tutorial211_Using-Performance-Counters-in-Csharp.html

(2) http://www.codeproject.com/KB/system/processescpuusage.aspx

(3) http://www.codeproject.com/kb/system/cpuusageByDudiAvramov.aspx

(4) http://social.msdn.microsoft.com/forums/en-US/winforms/thread/06b215f1-c6a6-4ebb-ba4e-d17484308741/

(5) http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter%28VS.90%29.aspx (Reference for PerformanceCounter)

If not, do you have  a code example using WMI?

Link:

(1) http://www.computerperformance.co.uk/vbscript/wmi_memory.htm

(2) (http://www.experts-exchange.com/Programming/Misc/Q_20927135.html

(3) http://www.codeproject.com/KB/system/MultiCPUUsage.aspx (CPU Usage in MultiProcessor machine).

(4) http://www.codeproject.com/KB/cs/System_Meter.aspx (For memory usage)

I remember working  with WMI in the past, but it was working on some computer and not  working on other(returning empty information). Im not sure exactly  what are the requirements for WMI to work on Windows XP and recent  OS(vista, 7).

Link explaining reason of slow working on Windows 7:

http://www.paessler.com/manuals/prtg7/wmi_sensors.htm

Link for WMI Code Creator: http://micksmix.wordpress.com/2010/02/02/the-wmi-code-creator/

-----------------------------------------------------
// CPU & Memory Usage by using PerformanceCounter
-----------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace CPU__MemoryCounter
{
    public partial class Form1 : Form
    {
        PerformanceCounter cpuCounter;
        PerformanceCounter ramCounter;
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.btnShow_Click(this, null);
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");          

            ramCounter = new PerformanceCounter("Memory", "Available MBytes");

            richTextBox1.Text = "CPU Use: "+getCurrentCpuUsage() + "\n" + "RAM Used: "+ getAvailableRAM();
        }

        public string getCurrentCpuUsage()
        {
            string cpu= cpuCounter.NextValue() + "%";
            return cpu;
        }

        public string getAvailableRAM()
        {
            string ram;
            int ramAvailable = Convert.ToInt32(ramCounter.NextValue());
            int ramUsed = 1024 - ramAvailable; //1024 if 1GB RAM
            return ram=ramUsed.ToString()+"MB";
        }

    }
}

Open in new window

0
 

Author Comment

by:Enill
Comment Utility
Thanks, ill give it a look.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now