|
|
Title | See on which processors the current process can run in Visual Basic 2005 |
Description | This example shows how to see on which processors the current process can run in Visual Basic 2005. |
Keywords | CPUs, number of CPUs, processors, number of processors, VB 2005 |
Categories | Windows |
|
|
Thanks to Alexander Klimoff (rusproject AT mail.ru).
When the form loads, the code uses Process.GetCurrentProcess to get a Process object representing the program's process. It calls that object's ProcessorAffinity method to get a bit mask listing the processors on which the process is allowed to run. It then loops through the bits in the mask to see which are allowed.
If the process is allowed to run on any processor, then the largest processor number guves the total number or processors.
|
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Dim txt As String = ""
Dim mask As Integer = 1
Dim affinity As Integer = _
Process.GetCurrentProcess.ProcessorAffinity()
For i As Integer = 1 To 32
If (affinity And mask) <> 0 Then
txt &= ", " & i
End If
mask <<= 1
Next i
If txt.Length > 0 Then txt = txt.Substring(2)
lblResult.Text = txt
End Sub
|
|
This technique should work on earlier versions of Visual Basic .NET, too.
|
|
|
|
|
|