Is there a way via .NET/C# to find out the number of CPU cores?
PS This is a straight code question, not a "Should I use multi-threading?" question! :-)
Is there a way via .NET/C# to find out the number of CPU cores?
PS This is a straight code question, not a "Should I use multi-threading?" question! :-)
It's rather interesting to see how .NET get this internally to say the least... It's as "simple" as below:
One option would be to read the data from the registry. MSDN Article On The Topic: http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.localmachine(v=vs.71).aspx)
The processors, I believe can be located here, HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor
I am reasonably sure the registry entry will be there on most systems.
Though I would throw my $0.02 in.
WMI queries are slow, so try to Select only the desired members instead of using Select *.
The following query takes 3.4s:
While this one takes 0.122s:
The the easyest way =
Environment.ProcessorCount
Exemple from Environment.ProcessorCount Property