473,563 Members | 2,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using WMI for system Metrics efficiantly

Hello all. I'm writting a little application to help me expand my knowledge
in a few different areas. The app is, as you might of guessed from the
subject, a system metrics display using WMI. For now I'm doing it with
Windows Forms, but later on I will be changing it to use DirectX's Overlays.

The question I have right now is how to get the information gathered from
WMI to easily, and efficiantly, update what is shown. I've tried using the
OnPaint method of the form to call an updating function to show the newest
information, but this turned out to be *very* slow. It does work, it just
takes ~15 seconds before it will show an update. This is unacceptable
concidering I'm using a 2.5GHz machine to test this on!

Below is the code I have so far:

using System;
using System.Collecti ons;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Manageme nt;
using System.Text;
using System.Windows. Forms;

namespace BNetSysMetrics
{
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}

protected override void OnPaint(PaintEv entArgs e)
{

UpdateStats();
base.OnPaint(e) ;
this.Invalidate ();
}

private bool UpdateStats()
{
#region Management Object Init
ManagementObjec tSearcher sysInfoQuery = new
ManagementObjec tSearcher("SELE CT * FROM Win32_Operating System");
ManagementObjec tCollection sysInfo = sysInfoQuery.Ge t();

ManagementObjec tSearcher diskInfoQuery = new
ManagementObjec tSearcher("SELE CT * FROM Win32_LogicalDi sk");
ManagementObjec tCollection diskInfo = diskInfoQuery.G et();

ManagementObjec tSearcher cpuInfoQuery = new
ManagementObjec tSearcher("SELE CT * FROM Win32_Processor ");
ManagementObjec tCollection cpuInfo = cpuInfoQuery.Ge t();

ManagementObjec tSearcher networkInfoQuer y = new
ManagementObjec tSearcher("SELE CT * FROM Win32_NetworkAd apter");
ManagementObjec tCollection networkInfo = networkInfoQuer y.Get();
#endregion

foreach (ManagementObje ct manObj in cpuInfo)
{
try
{
txtCPUClock.Tex t =
manObj["CurrentClockSp eed"].ToString() + "MHz";
txtCPULoad.Text = manObj["LoadPercentage "].ToString() +
"%";
}
catch (Exception)
{
}
}

foreach (ManagementObje ct manObj in sysInfo)
{
txtSysName.Text = manObj["CSName"].ToString();
txtRAMFree.Text =
Convert.ToUInt6 4(manObj["FreePhysicalMe mory"].ToString()) / 1000 + "MB";
txtRAMTotal.Tex t =
Convert.ToUInt6 4(manObj["TotalVirtualMe morySize"].ToString()) / 1000 + "MB";
txtVirtualMemor yFree.Text =
Convert.ToUInt6 4(manObj["FreeVirtualMem ory"].ToString()) / 1000 + "MB";
txtVirtualMemor yTotal.Text =
Convert.ToUInt6 4(manObj["TotalVisibleMe morySize"].ToString()) / 1000 + "MB";
}

return true;
}
}
}
If anyone can point me in the right direction to get this up to speed, I'd
be very appreciative.

Many thanks
Jul 8 '06 #1
3 3970
Most of the data you are retrieving is static, why do you need to get
properties of classes like Win32_NetworkAd apter, LogicalDisk,
Win32_Processor which rarely or never change.
Anyway, a LogicalDisk query is expensive when the system contains a floppy
drive, so I would suggest you to refine your query and exclude the flopy
drive(s).

Willy.

"Babillon" <Ba******@discu ssions.microsof t.comwrote in message
news:C8******** *************** ***********@mic rosoft.com...
| Hello all. I'm writting a little application to help me expand my
knowledge
| in a few different areas. The app is, as you might of guessed from the
| subject, a system metrics display using WMI. For now I'm doing it with
| Windows Forms, but later on I will be changing it to use DirectX's
Overlays.
|
| The question I have right now is how to get the information gathered from
| WMI to easily, and efficiantly, update what is shown. I've tried using the
| OnPaint method of the form to call an updating function to show the newest
| information, but this turned out to be *very* slow. It does work, it just
| takes ~15 seconds before it will show an update. This is unacceptable
| concidering I'm using a 2.5GHz machine to test this on!
|
| Below is the code I have so far:
|
| using System;
| using System.Collecti ons;
| using System.Collecti ons.Generic;
| using System.Componen tModel;
| using System.Data;
| using System.Drawing;
| using System.Manageme nt;
| using System.Text;
| using System.Windows. Forms;
|
| namespace BNetSysMetrics
| {
| public partial class Form1 : Form
| {
| public Form1()
| {
| InitializeCompo nent();
| }
|
| protected override void OnPaint(PaintEv entArgs e)
| {
|
| UpdateStats();
| base.OnPaint(e) ;
| this.Invalidate ();
| }
|
| private bool UpdateStats()
| {
| #region Management Object Init
| ManagementObjec tSearcher sysInfoQuery = new
| ManagementObjec tSearcher("SELE CT * FROM Win32_Operating System");
| ManagementObjec tCollection sysInfo = sysInfoQuery.Ge t();
|
| ManagementObjec tSearcher diskInfoQuery = new
| ManagementObjec tSearcher("SELE CT * FROM Win32_LogicalDi sk");
| ManagementObjec tCollection diskInfo = diskInfoQuery.G et();
|
| ManagementObjec tSearcher cpuInfoQuery = new
| ManagementObjec tSearcher("SELE CT * FROM Win32_Processor ");
| ManagementObjec tCollection cpuInfo = cpuInfoQuery.Ge t();
|
| ManagementObjec tSearcher networkInfoQuer y = new
| ManagementObjec tSearcher("SELE CT * FROM Win32_NetworkAd apter");
| ManagementObjec tCollection networkInfo =
networkInfoQuer y.Get();
| #endregion
|
| foreach (ManagementObje ct manObj in cpuInfo)
| {
| try
| {
| txtCPUClock.Tex t =
| manObj["CurrentClockSp eed"].ToString() + "MHz";
| txtCPULoad.Text = manObj["LoadPercentage "].ToString() +
| "%";
| }
| catch (Exception)
| {
| }
| }
|
| foreach (ManagementObje ct manObj in sysInfo)
| {
| txtSysName.Text = manObj["CSName"].ToString();
| txtRAMFree.Text =
| Convert.ToUInt6 4(manObj["FreePhysicalMe mory"].ToString()) / 1000 + "MB";
| txtRAMTotal.Tex t =
| Convert.ToUInt6 4(manObj["TotalVirtualMe morySize"].ToString()) / 1000 +
"MB";
| txtVirtualMemor yFree.Text =
| Convert.ToUInt6 4(manObj["FreeVirtualMem ory"].ToString()) / 1000 + "MB";
| txtVirtualMemor yTotal.Text =
| Convert.ToUInt6 4(manObj["TotalVisibleMe morySize"].ToString()) / 1000 +
"MB";
| }
|
| return true;
| }
| }
| }
|
|
| If anyone can point me in the right direction to get this up to speed, I'd
| be very appreciative.
|
| Many thanks
Jul 10 '06 #2
I would assume that Win32_Processor would not have static information, as
part of it's info is the current load. The current clock also changes (for
example with a laptop, the CPU clock is adjusted according to load).

As for the NetworkAdapter section, I'm removing that as the WMI isn't
complete in regards to what I was looking to poll.

What would be a better way of polling for new information than overriding
the OnPaint method? That's my main question really. Would following the
asynchronous examples in the MSDN library be the best bet?

"Willy Denoyette [MVP]" wrote:
Most of the data you are retrieving is static, why do you need to get
properties of classes like Win32_NetworkAd apter, LogicalDisk,
Win32_Processor which rarely or never change.
Anyway, a LogicalDisk query is expensive when the system contains a floppy
drive, so I would suggest you to refine your query and exclude the flopy
drive(s).

Willy.
Jul 10 '06 #3

"Babillon" <Ba******@discu ssions.microsof t.comwrote in message
news:40******** *************** ***********@mic rosoft.com...
|I would assume that Win32_Processor would not have static information, as
| part of it's info is the current load. The current clock also changes (for
| example with a laptop, the CPU clock is adjusted according to load).

Only 6 properties of a total of 32 of this class are dynamic....
Retrieving all dynamic properties of this class takes a couple of seconds as
the implementation must calculate the loadpercentage by taking two
snapshots, querying the static properties only take a couple of msecs.
|
| As for the NetworkAdapter section, I'm removing that as the WMI isn't
| complete in regards to what I was looking to poll.
|
| What would be a better way of polling for new information than overriding
| the OnPaint method? That's my main question really. Would following the
| asynchronous examples in the MSDN library be the best bet?
|

Much better is to implement a timer based (using a server timer) WMI query,
note that this will run on a worker thread, so you need to marshal the
update of the UI using Control.Invoke/BeginInvoke.

| "Willy Denoyette [MVP]" wrote:
|
| Most of the data you are retrieving is static, why do you need to get
| properties of classes like Win32_NetworkAd apter, LogicalDisk,
| Win32_Processor which rarely or never change.
| Anyway, a LogicalDisk query is expensive when the system contains a
floppy
| drive, so I would suggest you to refine your query and exclude the flopy
| drive(s).
| >
| Willy.
Jul 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6712
by: Edward Berard | last post by:
I am looking for tools that will analyze Java source code: -> I am primarily looking for metrics tools, e.g., tools that will tell me things like - Total lines of code - Total blank lines of code - Total commented lines of code
2
1695
by: Philippe C. Martin | last post by:
Hi, I am looking for an eric3/linux compatible alternative to checking code metrics (ex: true lines of code count) Regards, Philippe
9
3403
by: Markus Minichmayr | last post by:
Hello! Does anyone know a free tool to collect source code metrics like lines of code, no. of classes, etc.? Thanks Markus
6
1404
by: rh0dium | last post by:
Hi all, Basically I have a bunch of pluggins in a directory (METDIR). For each one of these templated pluggins I want to do a specific routine. Let's start with a basic template file example1.py ---------------- class example1: def __init__(self):
7
2168
by: Michael Deathya | last post by:
Hi, I am pulling over 400 different metrics from an Excel spreadsheet into Access (97). Conceptually, each row represents a single set of these 400 metrics. However, because of the 255 column limit in Access I am faced with a dilemma: Do I create two tables (1 with 255 columns, the other with 145) and a lookup table that stores which...
1
1717
by: khammond | last post by:
I'm an Enterprise Architect at a large insurance company. My background is Java, and I have brought in the CodePro metrics & code audit tool that runs automated metrics and code audits within Eclipse/WSAD. I'm interested in finding some similar tools for our C++ developers, who use Visual Studio as their IDE. Does anyone know of any C++...
0
1324
by: alexpowell | last post by:
Hi, I have set up a website to try and gather performance metrics for programming languages. The aim is simple: when I pick up a new language it always gives me 1001 ways of doing anything. Which is the best strategy for any language? What happens when a new version is released? With that in mind I have setup http://www.savagepm.co.uk/, a...
9
4594
by: dgleeson3 | last post by:
Hello All I have a txt file of strings of different lengths. I dont know how many strings are in the file. I have no problem reading the file and sending to the console (as below). To store the strings read, in a buffer, I had decided to use an array of strings.
1
1970
by: houh | last post by:
I have a java app that generates openoffice docs. When I run the java app in a shell script, as: export JAVA_HOME=/opt/corp/projects/metrics/Java/jdk1.5.0_07/bin/java export CONST_LOC=/home/methct/metrics/MGConfig/staging_8103.xml $JAVA_HOME view.main.MetricsMainFrame $CONST_LOC I can close the openoffice doc that are generated by the...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.