Number of processors
-
Hello, I have a quick question. Does anybody know how to find out how many processors are on the machine my app is running on? I've been drudging through System.Diagnostics to no avail, and I was wondering if anyone could point me in the right direction. Certainly there must be a constant or a property with a get accessor somewhere... but I just can't seem to find it. Thank you.
-Gatsby
-
Hello, I have a quick question. Does anybody know how to find out how many processors are on the machine my app is running on? I've been drudging through System.Diagnostics to no avail, and I was wondering if anyone could point me in the right direction. Certainly there must be a constant or a property with a get accessor somewhere... but I just can't seem to find it. Thank you.
-Gatsby
-
Hello, I have a quick question. Does anybody know how to find out how many processors are on the machine my app is running on? I've been drudging through System.Diagnostics to no avail, and I was wondering if anyone could point me in the right direction. Certainly there must be a constant or a property with a get accessor somewhere... but I just can't seem to find it. Thank you.
-Gatsby
Use WMI. Try the following: string cpuInfo = String.Empty; string temp = String.Empty; ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (cpuInfo == String.Empty) { cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); } } Console.WriteLine(cpuInfo); Console.ReadLine(); Don't forget to include the System.Management Namespace. Mustafa Ismail There are 10 kinds of people in this world. Those who understand binary and those who don't... ______________________ "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Hello, I have a quick question. Does anybody know how to find out how many processors are on the machine my app is running on? I've been drudging through System.Diagnostics to no avail, and I was wondering if anyone could point me in the right direction. Certainly there must be a constant or a property with a get accessor somewhere... but I just can't seem to find it. Thank you.
-Gatsby
Hi, that is a complex question, and I don't have the complete answer. What do you call a processor (there is some disagreement): - there is hyperthreading (basically 2 or more processor states sharing all other processor resources: functional units, caches, bus interface, ...), which you can disable through BIOS - there is multi-processing on a chip - there is multi-chip packages - and finally visible multi-processing: more than one IC package And obviously these can be combined. That's the technical view. Then there is the matter of software licensing, which exists in many forms, such as per processor, per system, per seat. There is room for debate! Environment.ProcessorCount "Gets the number of processors on the current machine" whatever that means. I expect/hope it to mean the largest number you can obtain taking into account all the forementioned possibilities. Also Process.ProcessorAffinity should have the value 2^n - 1 when n=Environment.ProcessorCount Hope this helps
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Hello, I have a quick question. Does anybody know how to find out how many processors are on the machine my app is running on? I've been drudging through System.Diagnostics to no avail, and I was wondering if anyone could point me in the right direction. Certainly there must be a constant or a property with a get accessor somewhere... but I just can't seem to find it. Thank you.
-Gatsby
System.Environment.ProcessorCount will give you number of them (actually in some cases will return number of cores not processors just like DeviceManager!) but if you need to know how many processors are on the machine really you can use WMI to found out and it would be somewhat like this
static int ProcessorCount() { ManagementObjectSearcher mos = new ManagementObjectSearcher(new SelectQuery("Win32\_Processor")); ManagementObjectCollection moc = mos.Get(); List processorIds = new List(); foreach (ManagementObject mo in moc) { string pID = mo.Properties\["ProcessorId"\].Value as string; if (!processorIds.Contains(pID)) processorIds.Add(pID); } return processorIds.Count; }
in the code, processorIds were checked and because the cores from the same processor have identical processorIds they're not gonna be counted twice or more hope the post would be useful good luck
-
System.Environment.ProcessorCount will give you number of them (actually in some cases will return number of cores not processors just like DeviceManager!) but if you need to know how many processors are on the machine really you can use WMI to found out and it would be somewhat like this
static int ProcessorCount() { ManagementObjectSearcher mos = new ManagementObjectSearcher(new SelectQuery("Win32\_Processor")); ManagementObjectCollection moc = mos.Get(); List processorIds = new List(); foreach (ManagementObject mo in moc) { string pID = mo.Properties\["ProcessorId"\].Value as string; if (!processorIds.Contains(pID)) processorIds.Add(pID); } return processorIds.Count; }
in the code, processorIds were checked and because the cores from the same processor have identical processorIds they're not gonna be counted twice or more hope the post would be useful good luck
Quite helpful! Problem solved. Thank you.
-Gatsby
-
Hi, that is a complex question, and I don't have the complete answer. What do you call a processor (there is some disagreement): - there is hyperthreading (basically 2 or more processor states sharing all other processor resources: functional units, caches, bus interface, ...), which you can disable through BIOS - there is multi-processing on a chip - there is multi-chip packages - and finally visible multi-processing: more than one IC package And obviously these can be combined. That's the technical view. Then there is the matter of software licensing, which exists in many forms, such as per processor, per system, per seat. There is room for debate! Environment.ProcessorCount "Gets the number of processors on the current machine" whatever that means. I expect/hope it to mean the largest number you can obtain taking into account all the forementioned possibilities. Also Process.ProcessorAffinity should have the value 2^n - 1 when n=Environment.ProcessorCount Hope this helps
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
That does help, quite alot! Combined with the other replies, I'm all set. Thanks!
-Gatsby
-
WMI has a wealth of hardware information. I'm not sure if theres a direct .Net class with the info in it.