There are two (or more) possible ways. One is to use a ManualResetEvent
as follows:
_Event = New ManualResetEvent(False); <-- Create event globally
Then, in your thread's start code:
_ProgressThread = New Thread(AddressOf ExecProc)
_ProgressThread.IsBackground = False
_ProgressThread.Start()
//the flow of execution should come here only after the thread has executed the method
//but its coming and executing this line after the thread has started.
_Event.WaitOne(); <-- This waits!
_Event.Close(); <-- This closes the event
Me.MainInit()
_ProgressThread = Nothing
In your thread method, you must call _Event.Set()
before the method returns in all cases, otherwise your application will be blocked.
Another way would be to have to thread invoke a delegate when finished. The code that you want to execute after the thread is done (Me.MainInit()
) would then go into the delegate method. This is actually quite elegant.
For example:
public delegate void ThreadDoneDelegate();
private void ExecProc()
{
ThreadDoneDelegate del = new ThreadDoneDelegate(TheThreadIsDone);
... // Do work here
this.Invoke(del);
}
private void TheThreadIsDone()
{
... // Continue with your work
}
Sorry, I do not speek VB fluently, so you'll have to convert this little C# snippet :-)