Results 1 to 10 of 10

Thread: [RESOLVED] [2005] Closing threads when program closes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Resolved [RESOLVED] [2005] Closing threads when program closes

    Hi Everyone,

    I have an application that has threads continually running in the background monitoring different settings. When the end users x's out of my program, the threads remain as a process in windows even though the GUI is closed.

    How do I terminate the threads when the GUI is x'd out, or how do I put logic into the threads to end if the GUI is closed?

    Thank you all in advance.

  2. #2
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] Closing threads when program closes

    like this, i think thats the right name for that event...maybe before close or something, im sure youl get it.....
    Code:
    private sub on_close(byval sender as object... blabla) handles me.onclosing
        yourthread.stop
    end sub

    Signatures suck

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2005] Closing threads when program closes

    vb Code:
    1. Private Sub form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    I use the form closing event to deal with things like this. If your using background workers to run the other threads then couldnt you use backgroundworkername.dispose() ?

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

    Re: [2005] Closing threads when program closes

    Set the IsBackground property of each Thread object to True. That is only safe to do if the threads are guaranteed not to leave anything in an invalid state if they are terminated implicitly.
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Re: [2005] Closing threads when program closes

    On the _FormClosed event, how would I stop a thread that is started like the below code?

    Code:
    Dim t As New Thread(AddressOf runContactThread)
            t.Start()


    JMC,

    Set the IsBackground property of each Thread object to True.
    How would I do that with the above code?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Re: [2005] Closing threads when program closes

    Does anyone know how to kill this thread on the _FormClosed event?

    Code:
    Dim t As New Thread(AddressOf runContactThread)
            t.Start()

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Closing threads when program closes

    Quote Originally Posted by Giraffe Frenzy
    Does anyone know how to kill this thread on the _FormClosed event?

    Code:
    Dim t As New Thread(AddressOf runContactThread)
            t.Start()
    Just use the IsBackground method like jmcilhinney mentioned.
    Code:
    Dim t As New Thread(AddressOf runContactThread)
    t.IsBackGround = True
            t.Start()
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Re: [2005] Closing threads when program closes

    I figured it out.

    On the _FormClosed event, type
    Code:
     End

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Closing threads when program closes

    Quote Originally Posted by Giraffe Frenzy
    I figured it out.

    On the _FormClosed event, type
    Code:
     End
    You shouldnt be using End. End is an old vb6 way of terminating the application. Why would you want to do that in the formclosed event?
    Did you even try the IsBackground property?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Re: [RESOLVED] [2005] Closing threads when program closes

    jmc and Atheist,

    thank you. I tried IsBackground as you suggested. This also worked. Based on what you said Atheist, I will use the IsBackground. Thank you all.

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