Search code examples
c#hyperthreading

How do I tell if the machine my program is running on uses hyperthreading? (C#)


I have some code that needs to know how many actual cores are available on my particular machine, and whether or not Hyperthreading is enabled.

Is there a way to do this in C#?

Update: The machines are a mix of XP and Vista

Update: Accessing 'Win32_Processor.NumberOfCores' or 'Win32_Processor.NumberOfLogicalProcessors' throws an exception (a ManagmentException with the message "Not Found") on one of the machines (but not all of them)


Solution

  • On Vista and higher you can use GetLogicalProcessorInformation via PInvoke to get the number of logical processor units.

    On Windows XP there's no way via C# to reliably differentiate hyper-threading from other multi-processor/core configurations. The WMI solution that someone posted will class multi-core processors as hyper-threaded.

    Prior to Vista the only reliable means is to check the CPUID of the processor. To use this you could create a native DLL that can be called from your managed code. The following Intel code sample would be a good starting point.