Threading Question

techmanbd

Junior Contributor
Joined
Sep 10, 2003
Messages
397
Location
Burbank, CA
First here is my code. Question at the bottom.

the sub that starts a test

Visual Basic:
Private Sub startTest()
        If Me.chckIVFAT.Checked Then
              runTestThread(NAME_OF_SUB)
        End If
      
        If Me.chckIVFELO.Checked Then
              runTestThread(NAME_OF_SUB)
        End If

        If Me.chckIVDO.Checked Then
              runTestThread(NAME_OF_SUB)
        End If
End sub

the sub to do the thread
Visual Basic:
  Private Sub runTestThread(NAME_OF_SUB)
        thrdTest = New Thread(AddressOf NAME_OF_SUB)
        thrdTest.IsBackground = True
        thrdTest.Start()
        Do
            Application.DoEvents()
        Loop While thrdTest.ThreadState.Running
    End Sub

So I don't have a LOT of lines of the thread, I made a sub to run the thread. How can I pass through the name of the sub to make the "addressof" for? I was thinking passing the name of the sub to start as a string, but how can I make sure the thread nows that is the sub I want to start as a thread? That is if that is a way to do it.
 
You could pass the function pointer in directly rather than doing the addressof operation in runTestThread:

Visual Basic:
runTestThread(AddressOf NAME_OF_SUB)

Private Sub runTestThread(pointerToSub)
        thrdTest = New Thread(pointerToSub)
        thrdTest.IsBackground = True
        thrdTest.Start()
        Do
            Application.DoEvents()
        Loop While thrdTest.ThreadState.Running
End Sub
 
Thanks for the reply.

That is probably going into the right direction. But I do get an error with that. pointerToSub is what is giving me the error. I get this message

'AddressOf' expression cannot be converted to 'System.Object' because 'System.Object' is not a delegate type.


So I probably to to make pointerToSub something other than an object, but can't find what to set it as.
 
Nevermind, I may have found it
Private Sub runTestThread(pointerToSub as ThreadStart)


I will give that a try and post later for success or more questions. Thanks for directing me in the right direction.
 
Re: Threading Question SUCCESS

it works. thanks buddy.

But did have to change this part


Do
Application.DoEvents()
Loop While thrdTest.ThreadState.Running

to this
Do
Application.DoEvents()
Loop While thrdTest.isAlive


as this would cause the code to keep going and then start the next thread at the same time.
 
Back
Top