Threading
-
I am having trouble getting my head around a Threading problem I have. I want to process some data while displaying a message stating that the data is being processed. So hear is some sample code that I have been trying.
thrd = New Thread(AddressOf ThreadProc)
thrd.start()
'process data
thrd.abort 'I want a way to know when process data is done.
Private Sub ThreadProc()
Dim load As New Loading load.ShowDialog() End Sub
But, When my process is done the showdialog does not go away until I click in the form or move the mouse. Any suggestions on how to do this. How can I get the showdialog to go away?
-
I am having trouble getting my head around a Threading problem I have. I want to process some data while displaying a message stating that the data is being processed. So hear is some sample code that I have been trying.
thrd = New Thread(AddressOf ThreadProc)
thrd.start()
'process data
thrd.abort 'I want a way to know when process data is done.
Private Sub ThreadProc()
Dim load As New Loading load.ShowDialog() End Sub
But, When my process is done the showdialog does not go away until I click in the form or move the mouse. Any suggestions on how to do this. How can I get the showdialog to go away?
Hi Cory, this is so wrong. It is almost always a bad idea to abort a thread since you typically don't know what it is doing at that specific point in time, and resources may be locked, memory allocated, etc. In this particular case, aborting the thread stops the message pump that was keeping the progress dialog alive (the pump is a loop running inside the ShowDialog method); by aborting the thread, the dialog just sits there, completely dead, what piece of code would be responsible of removing it? The correct way to handle this is by: 1. putting the time-consuming stuff in a thread or a BackgroundWorker (BGW are available since .NET 2.0), which you want to run to completion, hence no abort necessary; 2. showing the progress dialog in the normal manner, without using an extra thread. 3. letting the thread/BGW signal the progress dialog when it is done (this obviously requires caution about cross-thread violations, automatically solved by BGW, and solvable with Control.InvokeRequired/Control.Invoke on regular threads. 4. you may add real progress indication in the dialog; again a BGW is helping there 5. you may want to add a Cancel button; the proper way to handle a cancel request is by having the background job done in a collaborative way, typically by periodically checking a "please cancel" flag. Don't use Thread.Abort if you don't have to! I strongly suggest you study some BackgroundWorker examples. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets