Waiting for a thread to end before proceeding

voltzart

Member
Joined
Dec 28, 2011
Messages
11
Programming Experience
1-3
I created a class that is used to convert a video to an FLV. When the "Convert" method is called, a form is shown to display the conversion progress (I know it is probably bad OOP design to do it this way, but I digress...), and the conversion process begins.

My problem is that I need my main code to wait for the conversion process to end before it proceeds to the next line, and, at the moment, the conversion process happens on a different thread, so the main code keeps running concurrently.

Before I get into specifics, here is an outline of two theoretical solutions to my problem (I don't think either are possible):

Solution 1:
Keep the conversion process running on a separate thread and use a method like this (pseudocode): WaitFor(MyClass.Thread). This theoretical function would pause the code until the specified thread ends.

Solution 2:
Switch the conversion process over to the main thread so that the main calling code CAN'T progress until the conversion process has finished. Then I would need to find a way to have my form (which contains a ProgressBar) run on another thread or somehow update while the conversion process is going.


I really don't think either of these solutions are possible, so I'm very open to suggestions.

I don't want to use the event handling model for this particular case because my class is used in many different instances and it would get messy managing whether an event should be handled one way or another. I also don't want to pass a boolean value to the conversion function that determines what happens when it finishes because I want to keep this class as flexible and modular as possible.


So again, I'm open for any suggestions or input. Thanks.
 
I don't understand what the problem is with solution 2. If you have a progress bar update already written into the conversion sub why would it not work if you moved it to the main thread?
 
Here is one way to do it:

Private flConversionFinished As Boolean

Private Sub Convert()
    flConversionFinished = False
    Dim T As New Thread(New ThreadStart(AddressOf StartAsyncConversion)) : T.Start()
    Do Until flConversionFinished : My.Application.DoEvents() : Loop
End Sub

Private Sub StartAsyncConversion()
    ...
    flConversionFinished = True
End Sub
 
I don't understand what the problem is with solution 2. If you have a progress bar update already written into the conversion sub why would it not work if you moved it to the main thread?

The ProgressBar update is already on the main thread. The conversion process is on another thread. The update itself is separate from the conversion process; the Convert method boths creates the ProgressBar form and creates a new thread for the ConversionProcess sub to run on. So they are actually separate.

I was just thinking though...Solution 1 is completely out of the picture. If the main thread were to be paused somehow to wait for the conversion thread to finish, the ProgressBar form, which is on the main thread, wouldn't update anyway.

So, I need to find a way to run a form completely on a separate thread...I think. If I move the ConversionProcess to the main thread, I need the progress form to be able to update concurrently, and I can't think of another way to do that other than having it on a separate thread, but I have read that something like that isn't possible.


EDIT:

And Herman, that makes perfect sense. If I could figure out a way to get the ProgressBar form to update/redraw during the do loop I would be set. The only redraw methods I've used for forms are the one's that essentially "queue" a redraw, such as Invalidate(). Is there a way to get the form and its controls to update without leaving the do loop?
 
Back
Top