Results 1 to 6 of 6

Thread: how to stop threads ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    how to stop threads ?

    Hi i have a thread that runs multiple times:

    VB CODE
    Code:
        Private Sub pstart()
    
            Dim i As Integer
            For i = 0 To TextBox4.Text
    
                pthread = New System.Threading.Thread(AddressOf check)
                pthread.Start()
    
            Next
    
        End Sub
    It can be ran upto 200 times max, how would i stop this with a click of a button as "pthread.abort()" does not stop them all.

    Thanks for any help

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    Re: how to stop threads ?

    Quote Originally Posted by dbasnett View Post
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    Re: how to stop threads ?

    Quote Originally Posted by jmcilhinney View Post
    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

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width