Results 1 to 9 of 9

Thread: better way to exit a program with a thread

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210

    better way to exit a program with a thread

    hi. i just migrated to vb.net yesterday, and i have a bit of trouble exiting a program with a thread.

    the program estimates the euler's number in a thread. i can at any time suspend the thread and resume. there's no problem when i make the thread abort when the user clicks stop, but it seems to take longer than just suspending the thread. but there's a problem with suspending a thread. i don't know how to exit the program properly

    well, i have a solution but it doesn't seem like a proper way to exit the program. on closing of the form, i have
    VB Code:
    1. Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.         If t.IsAlive = True Then
    3.             On Error GoTo Quit
    4.             t.Abort()
    5.             t.Resume()
    6.             t.Abort()
    7.         End If
    8. Quit:
    9.     End Sub

    it works perfectly, even when the thread was not even triggered, when the thread is running, and when the thread is suspended. but if i just have the thread abort on exit, when the thread was suspended, it would give me an error. so i had it resume, then abort.

    is there a better way to doing this?
    thanks.

    EDIT:

    no wait it doesn't work perfectly
    it doesn't close properly when the thread was suspended.
    Last edited by nahya^^; Mar 4th, 2004 at 05:45 PM.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Use a thread from the ThreadPool and then call Application.Exit in the Closing event. The ThreadPool will clean itself up with the UI/Application thread closes.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    could you provide an example of using ThreadPool? i tried
    Dim t As New Threading.ThreadPool(AddressOf Calculate)
    but it gives me an underline on t, saying something about it being private.

    thanks.
    Last edited by nahya^^; Mar 4th, 2004 at 07:02 PM.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Threading.ThreadPool.QueueUserWorkItem(AddressOf Calculate)
    3.         MsgBox("testing")
    4.     End Sub
    5.  
    6.     Public Sub Calculate(ByVal state As Object)
    7.         Threading.Thread.CurrentThread.Sleep(4000)
    8.         MsgBox("tested")
    9.     End Sub

    You don't even really have to call Application.Exit. The ThreadPool will automatically close any threads when the main thread closes.
    Last edited by Edneeis; Mar 4th, 2004 at 07:06 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    hmm.
    it works when i try on a new project, but it gives me an underline on Calculate, saying something like Calculate doesn't have the same signature as delegate. what could be wrong there?

    never mind. i had to put Calculate(ByVal state as Object).
    i don't understand what that does though. but it works.

    EDIT:

    doh. how do i suspend and resume the thread?
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconThreadPooling.asp
    is too confusing..
    sorry to keep bothering you.
    Last edited by nahya^^; Mar 4th, 2004 at 07:36 PM.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Why are you wanting to suspend the thread?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    well. i guess i don't really have to. but i just want to learn how to do it. reading a bit from that msdn site it seems like if you use threadpool you can't terminate the thread... is that right?

    in this case, i'm trying to suspend the thread to give the user the chance to take a look at current estimate, then resume or clear the results.

    another thing: is there a way to automatically scroll down in an item box?

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I think there is a ScrollToItem method of a listbox, but I'm not sure. Yes unforuntately if you use the ThreadPool you have less control over the thread in regardes to suspending it and what not but that is mainly because the purpose of the ThreadPool is to allow the CLR to manage the thread. You shouldn't have to suspend the thread to get results though.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    heh. i found a way to abort the thread and exit but still not sure if this is the proper way:

    VB Code:
    1. Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.         If (t.IsAlive = True) And (threadActive = True) Then
    3.             t.Abort()
    4.             GoTo Quit
    5.         ElseIf (t.IsAlive = True) And (threadSuspend = True) Then
    6.             t.Resume()
    7.             t.Abort()
    8.             GoTo quit
    9.         End If
    10. Quit:
    11.     End Sub
    12. End Class

    i still don't like the idea of resuming suspended thread then aborting though.

    anyway. i couldn't find lstEuler.ScrollToItem, if that's what you meant.

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