Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread will not abort

Status
Not open for further replies.

shaminda

Programmer
Jun 9, 2000
170
US
In one of my multi threading programs I cannot get my threads to abort. I have about 5 threads and the threads starts one at a time. By the time the fifth thread is started the program gets really slow. The program will not execute pass LoadWheelStn1. How should I resolve this problem?
Code:
Private Sub LoadWheelStn1()
        If sPortOpenFlag = "T" Then
            Dim MyThread1 As New Thread(AddressOf ThreadStn1)
            MyThread1.Start()
        End If

    End Sub

    Private Sub ThreadStn1()

        Dim varRcvBuff1 As Integer

        Try
            Thread.Sleep(3000)
            Application.DoEvents()

            SyncLock Me
                varRcvBuff1 = AxAComm1.get_ValueD(900)
                'do stuff
            End SyncLock


            Do While (varRcvBuff1 <> 25)
                Thread.Sleep(331)
                Application.DoEvents()
                SyncLock Me
                    varRcvBuff1 = AxAComm1.get_ValueD(900)
                    'do stuff
                End SyncLock
            Loop

            If (varRcvBuff1 = 25) Then
                varRcvBuff1 = 0
                LoadWheelStn1()
                If Thread.CurrentThread.IsAlive Then
                    Thread.CurrentThread.Abort()
                End If
            End If

        Catch ex As Exception
            Select Case Err.Number
                Case 0

                Case Else

            End Select
        Finally
            Thread.CurrentThread.Abort()

        End Try
    End Sub
 
While you may already know this the most important thing to keep in mind is the number of threads is limited. Isn't some magical process that can just keep going. The point being while you can have it try to make as many different threads as you want it still depends on the processor and how much work you have each thread doing. There are still a finite number of CPU cycles that can be handled at a time. You don't have to put the code, but you might mention what the "do stuff" actually does and what kind of hardware you are running this on.

Second, Abort will not necessarily stop a thread. Make sure if there is a problem with your Do While that either it forces the condition to be met or you Exit Try. There isn't enough code to say if the Do While is the problem it is the most likely however. Your goal should always be to in some way allow any sub/function to end normally even a threaded one. Oh, and don't use an Exit Sub because it will skip your Finally.

Third, the way you have the Abort in the Try portion, assuming there is no code after it, get rid of it. It is unnecessary at that point to call an abort and you don't need it in the Finally either. Abort is meant to attempt to end the thread prematurely. You are already at the end of the thread it is proper to just allow the thread to end on its own by that point. Also, if it did abort then it wouldn't reach your Finally.

Finally, while I don't know this from experience I have heard that a background worker is more efficient with multiple threads. You might try that and see if you get better performance.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I should have said Exit Do or Exit Try.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top