Re: how to stop threads ?
I wouldn't use abort. This illustrates how I would do it.
Code:
Public Class Form1
Dim myThreads As New List(Of Threading.Thread)
Dim isRun As Boolean = False
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'start threads
isRun = True
For i As Integer = 1 To 4
Dim t As New Threading.Thread(AddressOf check)
t.IsBackground = True
t.Name = i.ToString
myThreads.Add(t)
t.Start()
Next
Debug.WriteLine("Started")
End Sub
Private Sub check()
Do While isRun
'simulate check
Threading.Thread.Sleep(100)
Loop
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
'stop threads
isRun = False 'tell threads to stop
For Each t As Threading.Thread In myThreads
t.Join()
Next
Debug.WriteLine("Ended")
End Sub
End Class
Re: how to stop threads ?
You've only got one variable so you only have one Thread reference so you can only Abort one thread. If you want to abort every thread then you need a reference to every Thread object. That means either an individual variable for each one or an array or collection containing them all.
That said, you really shouldn't be starting that many threads explicitly. With that many tasks to run you should be using the ThreadPool, either directly or indirectly.
Re: how to stop threads ?
Quote:
Originally Posted by
dbasnett
I wouldn't use abort. This illustrates how I would do it.
Code:
Public Class Form1
Dim myThreads As New List(Of Threading.Thread)
Dim isRun As Boolean = False
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'start threads
isRun = True
For i As Integer = 1 To 4
Dim t As New Threading.Thread(AddressOf check)
t.IsBackground = True
t.Name = i.ToString
myThreads.Add(t)
t.Start()
Next
Debug.WriteLine("Started")
End Sub
Private Sub check()
Do While isRun
'simulate check
Threading.Thread.Sleep(100)
Loop
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
'stop threads
isRun = False 'tell threads to stop
For Each t As Threading.Thread In myThreads
t.Join()
Next
Debug.WriteLine("Ended")
End Sub
End Class
Ok i added this to my code and no errors but when i hit the stop threads button the program becomes unresponsive and nothing stops.
Thanks
Re: how to stop threads ?
Quote:
Originally Posted by
jmcilhinney
You've only got one variable so you only have one Thread reference so you can only Abort one thread. If you want to abort every thread then you need a reference to every Thread object. That means either an individual variable for each one or an array or collection containing them all.
That said, you really shouldn't be starting that many threads explicitly. With that many tasks to run you should be using the ThreadPool, either directly or indirectly.
I tested this with 1000 and it ran fine with no lagg and no high mem or cpu usage and runs pretty damm fast, but i have limited it to 200 max. But yes its only 1 thread that runs multiple times as thats all thats needed.
Thanks
Re: how to stop threads ?
No, it's 200 threads (assuming you're limiting it correctly, which doesn't seem likely from what you've posted in the original post). You only have one variable. This is not the same thing as the Thread that the variable references. The first time through the loop you create a new thread and assign it to the variable. Since it is in the variable you can continue to reference that thread and manipulate it, specifically by calling the Start method on it. The next time through the loop, you create a new thread and assign it to the variable. The thread from the first time through the loop is now no longer referenced by any variables so you cannot further manipulate it (i.e. you cannot call the Abort method on it). However you still have two threads now even though you can't do anything with the first one.
As you continue through the loop, this continues, and you can only ever do anything with the last thread you created, but all the other threads are still there executing, un-abortable.
Did you look into the asynchronous API for network calls? It might look complicated, but it is at least as complicated as it looks. Threading is far more complicated than it first appears, and far far more complicated to get right than asynchronous programming on a single thread.