Results 1 to 7 of 7

Thread: Killing a thread from a different sub (need code)

  1. #1

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    Killing a thread from a different sub (need code)

    Before any one comments, ive read all the posts in this subject everything give comments but no CODE....

    my app's processing is done in a new thread when the user clicks a button

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.             Dim MyThread As New Thread(AddressOf runProg)
    3.            
    4.             MyThread.Name = "Primary Search Thread"
    5.             MyThread.Start()
    6.             Me.StatusPanel1Status.Text = "Scanning Please Wait!"
    7.  
    8.         End Sub

    ok this all works nicely

    i then have a sub for when the user closes the application it simple checks if some files exists and if they do to prompt the user wana filter them yes or no if they click no the application quits (but the thread doesnt)


    VB Code:
    1. Private Sub MainApp_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.             Dim result As MsgBoxResult = MsgBox("There are still files to filter, do you wish to complete this before exiting", MsgBoxStyle.YesNo, "Input")
    3.  
    4.             If result = MsgBoxResult.Yes Then
    5.                 RunFilter()
    6.             Else
    7.                 'exit the thread here
    8.             End If
    9.  
    10.         End Sub

    CODE examples would be really nice right now
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    How about declaring the Thread object OUTSIDE the subs (ie in the declarations section)?

    Then you can access it from anywhere.
    I don't live here any more.

  3. #3

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    HOW ??? Sorry this is first things ive done with threading
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Killing a thread from a different sub (need code)

    VB Code:
    1. Dim MyThread As Thread = New Thread(AddressOf runProg)
    2.  
    3. 'the other function minus the original thread declaration
    4. 'goes here
    5.  
    6. Private Sub MainApp_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    7.             Dim result As MsgBoxResult = MsgBox("There are still ......", MsgBoxStyle.YesNo, "Input")
    8.  
    9.             If result = MsgBoxResult.Yes Then
    10.                 RunFilter()
    11.             Else
    12.                 MyThread.Abort()
    13.             End If
    14.  
    15.         End Sub
    I don't live here any more.

  5. #5

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    Yeah done that but the application still seems to be running in my task manager. if it the case that the thread will wait until its run or will it quit stright away ????
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  6. #6

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    Ive checked the state of the thread and it keeps coming back as unstarted ???any ideas ?
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  7. #7
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    If the thread is running a loop, then abort won't close it down. At least, that's my experience.

    What I usually do, is use a public boolean.

    Then in I would make the loop like this:
    VB Code:
    1. While Not MyAbort
    2.     'do things here
    3. End While

    Then, to stop the thread I would just set the MyAbort=True.
    The loop will exit, and the thread will terminate.

    This usually works for me.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

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