Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,055 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,733 people online right now. Registration is fast and FREE... Join Now!




Closing a Thread

 
Reply to this topicStart new topic

Closing a Thread

bflosabre91
26 Feb, 2008 - 07:12 AM
Post #1

D.I.C Head
**

Joined: 22 Feb, 2008
Posts: 245



Thanked: 1 times
My Contributions
what i have is a separate thread that displays a loading screen while a query is executed. When the query is done, i want to close the thread which would close the loading form. is there a better way to manually close the thread without using thread.abort() ? i have a timer on the form and checks to see if the query is finished executing every 2 seconds, but now my problem is how to close the loading thread in a better way than thread.abort() because i keep getting the ThreadAbortException "Thread was being aborted"
any info i would really appreciate. thanks

This post has been edited by bflosabre91: 26 Feb, 2008 - 09:31 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Closing A Thread
26 Feb, 2008 - 03:32 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
It is supposed to throw that exception. Whenever you stop a thread, whether intentionally or accidentally that exception will be thrown. You just need to use a Try..Catch block, then check the type of exception that was thrown, if it was a ThreadAbortException swallow it and move on.

There is no way to stop a thread without that exception being thrown, .Net is just designed that way.
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Closing A Thread
26 Feb, 2008 - 05:14 PM
Post #3

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
Normally you shouldn't have to tell a thread to close. It should be able to do its job then finish automatically.

I don't see a need to use Thread.Abort() at all.

Normally, what I would do is have the thread checking a variable on whether it should close e.g.
vb

Private Sub CodeInThread()
Dim keepRunning As Boolean = True

While keepRunning 'keeps thread running forever
'...
'Do all the fancy code that you have inside your thread
'...

'We lock, because that is one way of ensuring that no other thread
'is accessing the same variables at the same time
SyncLock accessLock
If endThread Then
keepRunning = False
End If
End SyncLock
End While 'loop forever until keepRunning = False

'keepRunning now equals false
'put cleanup code here

'when the sub finishes, the thread ends
End Sub


So the above code needs access to some variables. So in the same class the following variables should be defined
vb

Dim accessLock As New Object
Dim endThread As Boolean = False


Then if we want to stop the thread, we just set the stopThread value to True
vb

Public Sub StopThread()
SyncLock accessLock
endThread = True
End SyncLock
End Sub


This way, there will be no exception, and also your thread has more control over what it does when it finishes. E.g. if it needs to finish, it may want to do some clean up first.

This post has been edited by Nayana: 26 Feb, 2008 - 05:46 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:28PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month