0

So I have 2 threads in my application which work fine except for the fact that they will continue taking up rather large amounts of my processor even after they are closed or aborted. This is how one of the loops look like along with some of the main declarations.

Dim Thread1 As System.Threading.Thread
Dim ThreadRunning As Boolean
Dim Thread1Running As Boolean = False

Sub Something()
    While True
        If ThreadRunning = True Then
            Try
                ...Actions which don't necessarily affect the thread
            Catch ex As Exception
            End Try
        ElseIf ThreadRunning = False Then
            If Thread1Running = True Then
                Thread1Running = False
                Thread1.Abort()
            Else
            End If
        End If
    End While
End Sub

Here is the code I used to start the thread.

Thread1Running = True
Dim Thread1 As New System.Threading.Thread(AddressOf Something)
Thread1.IsBackground = True
Thread1.Priority = ThreadPriority.Lowest
Thread1.Start()

Here is the code I use to stop the thread.

ThreadRunning = False

The actions in the thread need threads since timers were too slow for the task(even at a 1 ms interval). The actions are performed fine except for when I abort the thread. In this case the thread will close and the CPU usage will go from around 25% to 0% for this program but then it will crash with a CLR error. When I aborted the thread from outside the sub it would still leave the program CPU usage at 25% which is what I'm trying to avoid. So my main question is: Is it possible to close the thread and reopen it later without it crashing, and so that while it is closed it won't use up 25% of the CPU(or some other rather high CPU performance)? If this isn't enough information I will provide more but I thing this will suffice... hopefully. Thanks for any help.

  • 1
    [**Don't use `Thread.Abort` !**](http://stackoverflow.com/questions/1559255/whats-wrong-with-using-thread-abort) [It is *evil*](http://haacked.com/archive/2004/11/12/how-to-stop-a-thread.aspx/). Also, you're showing all the code managing the thread, but the problem lies in what the thread is *actually doing*, which you've not shown. Just because something is in a thread doesn't mean it will use less CPU - it just means your GUI will still be functional (here). A `while(1) { }` loop is still going to fully consume one CPU core. – Jonathon Reinhart May 21 '14 at 06:26
  • I see. Thanks for the quick reply. So is there an alternative for Thread.Abort? As for the code I don't think it can necessarily crash the application if the thread closes from what I've tested with. Closing threads when exiting the application isn't a problem either. It's mainly that the threads are using that percentage while they are inactive or supposedly aborted/suspended. I am trying to find a way to possibly suspend their CPU usage/running until it is started again(I've tried Thread.Abort and Thread.Suspend/Resume but neither worked correctly or I used them wrong). –  May 21 '14 at 06:45
  • I think these are two separate issues: 1) Thread CPU usage (which is totally dependent on the code you're not showing, and 2) `Thread.Abort` / how to exit background threads. – Jonathon Reinhart May 21 '14 at 06:47
  • Usually if I use any thread which loops it will use up 25% every time. My problem is that if I try stopping the thread it will either continue to use up that same amount of CPU even though it's supposedly stopped or it will crash the application. Sorry if I'm just repeating what I said before since I really don't know where to start on fixing this if it IS fixable. –  May 21 '14 at 06:50
  • Again, the CPU usage has to with whatever code your thread is running in a loop. Simple as that. If you don't want it fully consuming one core (of your 4-core CPU) then you need to change *the code in the loop* - which you haven't shown. – Jonathon Reinhart May 21 '14 at 06:53
  • If you don't care about the CPU usage, and just want to know how to reliably stop a background thread, then that is a different issue. Right now, you're asking two tangentially-related questions. Have you looked into [`BackgroundWorker`](http://msdn.microsoft.com/en-us/library/vstudio/system.componentmodel.backgroundworker), by the way? – Jonathon Reinhart May 21 '14 at 06:54
  • Thanks for you help and I finally figured out what I was looking for through a bit of trial and error. The result was somewhat weird but it works. Might as well share if anyone else might stumble across a similar issue(I'll post it when it lets me after 8 hours). Thanks for the help once again Jonathon. –  May 21 '14 at 07:05
  • Perhaps, but if your code still has `Thread.Abort()` it will be less than ideal, and not really a "good example". There are documented ways of doing this *the right way*. – Jonathon Reinhart May 21 '14 at 07:09
  • Also I just had it exit the while and the sub rather than use Thread.Abort and that seems to have fixed it(Just to be clear I completely took out Thread.Abort so thanks for that tip). I can't post the answer yet due to lack of reputation but I'll do it as soon as I can. –  May 21 '14 at 07:10

1 Answers1

0

Here's the solution I found which worked.

While True
        If ThreadRunning = True Then
            Try
                ...Code stuff
            Catch ex As Exception
            End Try
        ElseIf ThreadRunning = False Then
            Exit While
        End If
    End While
Exit Sub

Apparently checking it this way made it so that 1. The thread doesn't hang. 2. It fully exits the thread and prevents it from using the CPU while closed. And 3. It allows for reusing the threads through the same method as shown above.