Wait for thread to complete

I have 3 modules that are called each with a timer and a thread. I want the 2nd one to wait until the first thread is done running and the 3rd one to wait until the 2nd one is done running.

Should the thread be structured in such a way or should a while loop be used to keep the successive thread from starting such as the following between threads and timers

Main window sets a variable to IsThreadRunning = True

Start new process with timer and thread
When thread ends set IsTheradRunning = False (does this in the Action method of timer)

While IsThreadrunning
app.doevents
wend

Start next process with timer and thread
When thread ends set IsThreadRunning = False

While IsThreadrunning
app.doevents
wend

All three threads can start, then the second and third ones immediately pause themselves. The first thread, as it completes, can resume the second thread, which, when it also completes, resumes the third thread.

No need for loops or other complication.

I actually need to wait for the first one to complete or I get errors because of the second process cannot start till the first one completes.

So the first one can start the second one. And why cannot the second one start and then immediately pause itself?

It does seem like 1 should call 2, and 2 should call 3. A CriticalSection could be useful here too. Each would all lock on the same object. When called in order, the first would get the lock, while the second and third would be blocked. Once the first releases the lock, the second would immediately unlock. And same for the second and third.

it looks like

thread run
method1
method2
method3

instead of
thread1 run method1 → thread2 run method2 → thread3 run method3

why do you need 3 threads when they run sequential?

2 Likes

Why use threads at all when the main thread waits for all three tasks to complete?

@Carsten_Belling
because without the app (can) hang ? …

It’s already in a bad way with DoEvents unless this is a console app. He should do as @Thom_McGrath said and have the process run 1 through 3 with each calling the next. Or, alternatively, sucblass thread and include an event for finished and call the next one there.

hey, this both was not related.
it was just the answer about

“Why use threads at all when the main thread waits for all three tasks to complete?”

At a guess, each thread may perform different functions which may need to run independently of the entire 3-thread process elsewhere.

better expressed my meaning was using 1 thread instead of 3 if the methods run sequential in this case.