|
-
Mar 4th, 2004, 05:37 PM
#1
Thread Starter
Addicted Member
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:
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If t.IsAlive = True Then
On Error GoTo Quit
t.Abort()
t.Resume()
t.Abort()
End If
Quit:
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.
-
Mar 4th, 2004, 06:32 PM
#2
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.
-
Mar 4th, 2004, 06:49 PM
#3
Thread Starter
Addicted Member
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.
-
Mar 4th, 2004, 07:02 PM
#4
VB Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Threading.ThreadPool.QueueUserWorkItem(AddressOf Calculate)
MsgBox("testing")
End Sub
Public Sub Calculate(ByVal state As Object)
Threading.Thread.CurrentThread.Sleep(4000)
MsgBox("tested")
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.
-
Mar 4th, 2004, 07:20 PM
#5
Thread Starter
Addicted Member
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.
-
Mar 4th, 2004, 09:11 PM
#6
Why are you wanting to suspend the thread?
-
Mar 4th, 2004, 09:24 PM
#7
Thread Starter
Addicted Member
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?
-
Mar 5th, 2004, 01:06 AM
#8
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.
-
Mar 5th, 2004, 01:34 AM
#9
Thread Starter
Addicted Member
heh. i found a way to abort the thread and exit but still not sure if this is the proper way:
VB Code:
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If (t.IsAlive = True) And (threadActive = True) Then
t.Abort()
GoTo Quit
ElseIf (t.IsAlive = True) And (threadSuspend = True) Then
t.Resume()
t.Abort()
GoTo quit
End If
Quit:
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|