473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using System.Management a faux pas?

Is using System.Management in general a faux pas? It seems, after some
web searching, that there is a fair amount of resistance to it because
of speed and portability.

I need to know how many CPU sockets there are. I'm using
System.Management to do this. It takes about two seconds to do this.
(Which, yes, is full-on crap for speed.) But what is the better way to
determine the number of CPU sockets in Windows version >= 5 with
a .NET >= 2.0 application?

Jun 18 '07 #1
12 2365
not_a_commie,

With the System.Management namespace, you are trading off performance
for ease-of-use, and maintainability (through a unified object model and
query syntax). I am sure that there is much faster code to find this out,
but it is most likely done with interop, which if not done correctly, can
impact ease-of-use and maintainability.

So in the end, it is a trade off, and like all trade offs, you have to
make the decision which one is better for your application's needs.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"not_a_commie" <no********@gmail.comwrote in message
news:11*********************@d30g2000prg.googlegro ups.com...
Is using System.Management in general a faux pas? It seems, after some
web searching, that there is a fair amount of resistance to it because
of speed and portability.

I need to know how many CPU sockets there are. I'm using
System.Management to do this. It takes about two seconds to do this.
(Which, yes, is full-on crap for speed.) But what is the better way to
determine the number of CPU sockets in Windows version >= 5 with
a .NET >= 2.0 application?

Jun 18 '07 #2
"not_a_commie" <no********@gmail.comwrote in message
news:11*********************@d30g2000prg.googlegro ups.com...
Is using System.Management in general a faux pas? It seems, after some
web searching, that there is a fair amount of resistance to it because
of speed and portability.

I need to know how many CPU sockets there are. I'm using
System.Management to do this. It takes about two seconds to do this.
(Which, yes, is full-on crap for speed.) But what is the better way to
determine the number of CPU sockets in Windows version >= 5 with
a .NET >= 2.0 application?

Environment.ProcessorCount, returns the number of processors, but this is
not what you are looking for.
Getting the CPU unit count from WMI should however not take more than 50
msecs.
What OS and what WMI class are you using for this.

Willy.

Jun 18 '07 #3
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:uh**************@TK2MSFTNGP06.phx.gbl...
"not_a_commie" <no********@gmail.comwrote in message
news:11*********************@d30g2000prg.googlegro ups.com...
>Is using System.Management in general a faux pas? It seems, after some
web searching, that there is a fair amount of resistance to it because
of speed and portability.

I need to know how many CPU sockets there are. I'm using
System.Management to do this. It takes about two seconds to do this.
(Which, yes, is full-on crap for speed.) But what is the better way to
determine the number of CPU sockets in Windows version >= 5 with
a .NET >= 2.0 application?


Environment.ProcessorCount, returns the number of processors, but this is
not what you are looking for.
Getting the CPU unit count from WMI should however not take more than 50
msecs.
What OS and what WMI class are you using for this.

Willy.

Following:
int processors;
SelectQuery query = new SelectQuery("select Deviceid from
Win32_Processor");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query))
{
ManagementObjectCollection processor= searcher.Get();
processors = processor.Count;
}

takes 30...40msecs. when run on my box.
Note that the very first call can take some more time on some OS versions,
as the system has to start WMI (when not already).
started, anyway, this should be *much* less than 2 seconds, provided your
system is not running low on CPU or/and memory resources.

Willy.

Jun 18 '07 #4
I'm using WinXP SP2 on a core 2 duo. I'm thinking that my timer is
getting thrown off because this is being loaded in a static
constructor with other assemblies being loaded at the same time.
Here's the code:

if (Environment.OSVersion.Version.Major >= 6 ||
Environment.OSVersion.Platform == PlatformID.Unix)
return; // the bug we're working around exists in WinXP and older

ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection mos = mc.GetInstances(); // get all the
processors from the device manager
List<stringcpuSockets = new List<string>();

foreach (ManagementObject mo in mos)
{
string socketDesc =
mo.SystemProperties["SocketDesignation"].Value.ToString();
if(!cpuSockets.Contains(socketDesc))
cpuSockets.Add(socketDesc);
}

if (cpuSockets.Count 1)
{
// my special handling here
}

Jun 18 '07 #5
So can I do something like this?

"select unique SocketDesignation from Win32_Processor"

Jun 18 '07 #6
So I thought that

"select SocketDesignation from Win32_Processor group by
SocketDesignation"

would work (I would just use the count rows returned from that query).
Alas, it does not. Is SocketDesignation not the same as
["SocketDesignation"].Value? Or does the "group by" in WQL just behave
in a fishy manner? This query throws an exception:

"select count(*) from Win32_Processor group by SocketDesignation"

Jun 18 '07 #7
"not_a_commie" <no********@gmail.comwrote in message
news:11*********************@x35g2000prf.googlegro ups.com...
So can I do something like this?

"select unique SocketDesignation from Win32_Processor"

No, but if you are only interested in a single CPU's properties, you can do
the following to get at a specific instance.

string objPath = "Win32_Processor.DeviceId='CPU0'";
string socketType;
using(ManagementObject service = new ManagementObject( new
ManagementPath(objPath)))
{
socketType = service.Properties["SocketDesignation"].Value.ToString();
}

Willy.

Jun 19 '07 #8
"not_a_commie" <no********@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
So I thought that

"select SocketDesignation from Win32_Processor group by
SocketDesignation"

would work (I would just use the count rows returned from that query).
Alas, it does not. Is SocketDesignation not the same as
["SocketDesignation"].Value? Or does the "group by" in WQL just behave
in a fishy manner? This query throws an exception:

"select count(*) from Win32_Processor group by SocketDesignation"

While WQL looks like SQL it is not a full fledged SQL.

Willy.

Jun 19 '07 #9
No, but if you are only interested in a single CPU's properties, you can do
the following to get at a specific instance.
...
socketType = service.Properties["SocketDesignation"].Value.ToString();
I'm interested in the count of unique SocketDesignation values --
nothing else. It's creating instances of these objects that takes the
time, not the WQL. I was hoping to get all the information I needed
using WQL.

Jun 19 '07 #10
"not_a_commie" <no********@gmail.comwrote in message
news:11********************@j4g2000prf.googlegroup s.com...
>No, but if you are only interested in a single CPU's properties, you can
do
the following to get at a specific instance.
...
socketType = service.Properties["SocketDesignation"].Value.ToString();

I'm interested in the count of unique SocketDesignation values --
nothing else. It's creating instances of these objects that takes the
time, not the WQL. I was hoping to get all the information I needed
using WQL.


What takes time is starting up the WMI service (when not actually running)
and connecting to the WMI Service (through LPC for local connections and
DCOM over SMB for remote connections). Creating an instance takes no
measurable time as these are static instances , stored in WMI's metabase. On
the slowest box available, the query below took less than 700msecs. to
finish the first query, following queries, that is after WMI was running,
finished in less than 150msec. Did you actually instrumented your code, in
order to see what is taking most of the time?

Note that GetInstances returns all instances of the class, that means that
the number of instances IS the number of sockets.

ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection mos = mc.GetInstances();
int sockets = mos.Count;

Willy.

Jun 19 '07 #11
Note that GetInstances returns all instances of the class, that means that
the number of instances IS the number of sockets.
That's not true. I have a dual core system. I have two instances of
Win32_Processor. The SocketDesignation.Value on both of them specify
the same thing, which is correct: there is only one socket on my box.

Jun 19 '07 #12
"not_a_commie" <no********@gmail.comwrote in message
news:11*********************@o11g2000prd.googlegro ups.com...
>Note that GetInstances returns all instances of the class, that means
that
the number of instances IS the number of sockets.

That's not true. I have a dual core system. I have two instances of
Win32_Processor. The SocketDesignation.Value on both of them specify
the same thing, which is correct: there is only one socket on my box.
Oh I see, this class implementation looks terribly broken on XP, you have
two instances and only one socket!.
On Vista , it returns one instance per socket and the field "NumberOfCores"
returns 2 for a Core Duo, while NumberOfLogicalProcessors has a value of 2
or 4 depending whether each core is HT enabled, and 'SocketDesignation' has
the string that describes the CPU socket, like "Socket 939" for AMD Athlon
X2 or "Socket 478" or "Socket 479" for core duo's.
I'm afraid you will have to look at calling the Win32 API's to get reliable
info about this.

Willy.


Jun 19 '07 #13

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

Similar topics

2
13992
by: Paul Gronka | last post by:
I've got a VB.NET windows application (written in VS .NET 2003) that makes a call to WMI for retrieving the MAC Address from the client's PC. It works on 4 out of the 5 PC's tested so far. All...
2
11388
by: Brian Worth | last post by:
I have just upgraded from VB 4.0 to VB .NET 2002. One program under VB 4.0 was able to shut down or restart the (windows XP) machine using a series of API calls. (Getlasterror, GetCurrentProcess,...
1
1909
by: james | last post by:
I am attempting to write a VB.net program to alter remote registeries. I am familiar with VBScript and so I was using WMI to connect to remote machines. How would I do this in VB.Net? I got this...
1
3681
by: lecnac | last post by:
Sorry for the repost. I must have done something wrong when I tried to post my reply (I can't seem to find it). Anyway, I'd really appreciate any help that anyone could provide. My issue is...
6
1723
by: varois83 | last post by:
Hi I am currently learning the faux columns technique from various tutorials but none of them tells me how to create a 774 px band in 3 colors in photoshop. I understand I can create 3...
1
5628
by: remya1000 | last post by:
from my system i need to open a calculator in remote machine. and i'm using Vb.net and WMI. i need to pop up the calculator in remote machine, while i run one program in my system. while running in...
3
16138
by: Marco Shaw | last post by:
I've got some C# code to create a custom PowerShell cmdlet with these statements: .... using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; .... ...
0
1635
by: garks | last post by:
I am using Exchange 2003(Exchange Hosting), C#. I am trying to get Mailbox usage using WMI But I follows the coding below, still cannot get the Mailbox usage. Even though, I predefined the...
4
1665
by: TamusJRoyce | last post by:
Looking at various Holy Grail techniques for html layouts, I came across Faux Positioning Layout. What I'm curious about is the differences between the two. I have no understanding of the Holy...
0
7074
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...
0
7273
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,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
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...
1
5000
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...
0
4667
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
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
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 ...
0
374
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.