Link to home
Start Free TrialLog in
Avatar of jr_barros_jr
jr_barros_jr

asked on

Set a CPU for Thread in VB.net

Hi,

I would like to know how can I specify a CPU that whill run a specific thread?
Scenario:
I have 2 threads. I woukd that CPU1 runs the Thread1 and CPU1 runs Thread2 and my application is still running in my CPU0.
ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Todd Gerbert
Found this bit of code in C# here: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a55733db-cac0-4ccb-a3cc-a584742b41f9/, seems to test okay.
Imports System.Threading
Imports System.Runtime.InteropServices
Module Module1
    <DllImport("kernel32")> _
    Public Function GetCurrentThreadId() As Integer
    End Function
 
    Sub Main()
        Dim thrd As New Thread(AddressOf ThreadProc)
 
        thrd.Start()
        thrd.Join(20000)
    End Sub
    Sub ThreadProc()
        Dim myThreadProc As ProcessThread = Nothing
        Dim myThreadId As Integer = GetCurrentThreadId()
 
        For Each pt As ProcessThread In Process.GetCurrentProcess().Threads
            If pt.Id = myThreadId Then
                myThreadProc = pt
                Exit For
            End If
        Next
 
        myThreadProc.ProcessorAffinity = 1
 
        Do
            Console.WriteLine("Thread {0} running on processor 1", myThreadId)
            Thread.Sleep(1000)
        Loop
    End Sub
End Module

Open in new window