473,499 Members | 1,579 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.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Management;
using System.Text;
using System.Windows.Forms;

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

protected override void OnPaint(PaintEventArgs e)
{

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

private bool UpdateStats()
{
#region Management Object Init
ManagementObjectSearcher sysInfoQuery = new
ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
ManagementObjectCollection sysInfo = sysInfoQuery.Get();

ManagementObjectSearcher diskInfoQuery = new
ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
ManagementObjectCollection diskInfo = diskInfoQuery.Get();

ManagementObjectSearcher cpuInfoQuery = new
ManagementObjectSearcher("SELECT * FROM Win32_Processor");
ManagementObjectCollection cpuInfo = cpuInfoQuery.Get();

ManagementObjectSearcher networkInfoQuery = new
ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter");
ManagementObjectCollection networkInfo = networkInfoQuery.Get();
#endregion

foreach (ManagementObject manObj in cpuInfo)
{
try
{
txtCPUClock.Text =
manObj["CurrentClockSpeed"].ToString() + "MHz";
txtCPULoad.Text = manObj["LoadPercentage"].ToString() +
"%";
}
catch (Exception)
{
}
}

foreach (ManagementObject manObj in sysInfo)
{
txtSysName.Text = manObj["CSName"].ToString();
txtRAMFree.Text =
Convert.ToUInt64(manObj["FreePhysicalMemory"].ToString()) / 1000 + "MB";
txtRAMTotal.Text =
Convert.ToUInt64(manObj["TotalVirtualMemorySize"].ToString()) / 1000 + "MB";
txtVirtualMemoryFree.Text =
Convert.ToUInt64(manObj["FreeVirtualMemory"].ToString()) / 1000 + "MB";
txtVirtualMemoryTotal.Text =
Convert.ToUInt64(manObj["TotalVisibleMemorySize"].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 3962
Most of the data you are retrieving is static, why do you need to get
properties of classes like Win32_NetworkAdapter, 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******@discussions.microsoft.comwrote in message
news:C8**********************************@microsof t.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.Collections;
| using System.Collections.Generic;
| using System.ComponentModel;
| using System.Data;
| using System.Drawing;
| using System.Management;
| using System.Text;
| using System.Windows.Forms;
|
| namespace BNetSysMetrics
| {
| public partial class Form1 : Form
| {
| public Form1()
| {
| InitializeComponent();
| }
|
| protected override void OnPaint(PaintEventArgs e)
| {
|
| UpdateStats();
| base.OnPaint(e);
| this.Invalidate();
| }
|
| private bool UpdateStats()
| {
| #region Management Object Init
| ManagementObjectSearcher sysInfoQuery = new
| ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
| ManagementObjectCollection sysInfo = sysInfoQuery.Get();
|
| ManagementObjectSearcher diskInfoQuery = new
| ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
| ManagementObjectCollection diskInfo = diskInfoQuery.Get();
|
| ManagementObjectSearcher cpuInfoQuery = new
| ManagementObjectSearcher("SELECT * FROM Win32_Processor");
| ManagementObjectCollection cpuInfo = cpuInfoQuery.Get();
|
| ManagementObjectSearcher networkInfoQuery = new
| ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter");
| ManagementObjectCollection networkInfo =
networkInfoQuery.Get();
| #endregion
|
| foreach (ManagementObject manObj in cpuInfo)
| {
| try
| {
| txtCPUClock.Text =
| manObj["CurrentClockSpeed"].ToString() + "MHz";
| txtCPULoad.Text = manObj["LoadPercentage"].ToString() +
| "%";
| }
| catch (Exception)
| {
| }
| }
|
| foreach (ManagementObject manObj in sysInfo)
| {
| txtSysName.Text = manObj["CSName"].ToString();
| txtRAMFree.Text =
| Convert.ToUInt64(manObj["FreePhysicalMemory"].ToString()) / 1000 + "MB";
| txtRAMTotal.Text =
| Convert.ToUInt64(manObj["TotalVirtualMemorySize"].ToString()) / 1000 +
"MB";
| txtVirtualMemoryFree.Text =
| Convert.ToUInt64(manObj["FreeVirtualMemory"].ToString()) / 1000 + "MB";
| txtVirtualMemoryTotal.Text =
| Convert.ToUInt64(manObj["TotalVisibleMemorySize"].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_NetworkAdapter, 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******@discussions.microsoft.comwrote in message
news:40**********************************@microsof t.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_NetworkAdapter, 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
6706
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...
2
1684
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
3387
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
1397
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...
7
2151
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...
1
1706
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...
0
1317
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...
9
4589
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...
1
1963
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...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6893
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...
0
7386
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...
0
5468
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,...
0
4599
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...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.