Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSee on which processors the current process can run in Visual Basic 2005
DescriptionThis example shows how to see on which processors the current process can run in Visual Basic 2005.
KeywordsCPUs, number of CPUs, processors, number of processors, VB 2005
CategoriesWindows
 
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.
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated