How to stop a thread

tukmolins

Member
Joined
Oct 21, 2011
Messages
12
Programming Experience
1-3
Hello again. I'm having a hard time stopping a thread. Whenever i close my main form the application doesn't stop because there is a thread still running. I tried every solution I can find in the web but i can't make it work. Can someone help me with this?


Public Class Main
    Dim ServerThread As New System.Threading.Thread(AddressOf netLink.Main)




    Private Sub Main_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        SyncLock ServerThread
            ServerThread.Abort()
        End SyncLock

 Call netLink.stopThread() 'I called a public sub in my module to change the value of a Boolean variable to false so that all "While loops" can stop
    End Sub


    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ServerThread.Start()
    End Sub


End Class



 
If you set the IsBackground property of the Thread to True before you call its Start method then it will be terminated implicitly when your app exits. It's only safe to do that if there will never be any cleanup to be done no matter where the thread terminates. If there are or could be times where you should perform some cleanup before terminating the thread then you should call use a foreground thread, call Abort explicitly and handle the ThreadAbortException that gets thrown.
 
Hi all.
I see that post is old, but I have similar question about threads.

I read that using thread.Abort() is bad solution. Why?
Is there some good examples of safetly closing threads?

I often use thread (form) created from main form

Private Sub ShowMsg()
Dim frmMsg = New frmInit
frmMsg.ShowDialog()
End Sub


Somewhere in the man form code create thread and start:

trdMessage = New Thread(New ThreadStart(AddressOf ShowMsg))
trdMessage.IsBackground = True
trdMessage.Start()


And after some event i use
trdMessage.Abort()

Is that good practice, someone use SyncLock and other methods to work with threads?
What is the best,safetly method?

Only problem here is how to prevent closing the main form when thread(form) is active???
 
The reason that Abort is considered bad practice is that it can interrupt your thread at absolutely any point. You have no idea what state the thread is in so you have no idea whether it is in an unstable state, e.g. it may have a file open at the time. If you do call Abort and there is a chance that the thread could be in an unstable state then you should handle the ThreadAbortException that is thrown and do any appropriate cleanup. It is preferable to set some flag somewhere that the thread itself can test and only terminate itself at a point where it can perform its own cleanup and leave everything neat and tidy.
 
Please, Can you give some code example of that.
Or if you have a time to see my code above, and give the right solution to open new form from thread and safetly close that form.
The whole thing that I need is to show new form with marquee progress bar during some process which executing in the main form?
 
You don't open the new form from the background thread. In fact, you do completely the opposite. You open the new form with the progress bar first and then that form starts the new thread. You should take this background task out of the main form and put it into the new form or maybe even some separate class. You should probably use a BackgroundWorker, do the work in the DoWork event handler and close the form in the RunWorkerCompleted event handler. If you want to be able to cancel the task, the BackgroundWorker provides a mechanism to do so.
 
You should take this background task out of the main form and put it into the new form or maybe even some separate class.
Sorry, but I don't understand...
I need to create new form when some event occur in main form. How to check that and put into new form???
 
Here's an example. This application populates a TreeView with all the folders under the Program Files folder. It displays a dialogue with a Marquee ProgressBar and that dialogue crawls the file system using a BackgroundWorker. The dialogue provides the ability to cancel the operation and once the task is complete, the main form can retrieve the data from the dialogue.
 

Attachments

  • BackgroundWorker Demo.zip
    16.6 KB · Views: 35
Back
Top