I have written a thread which I’ve started using the start method but I’m not able to know when the thread has done executing the method and destroy the thread object.
_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.
Me.MainInit()
_ProgressThread = Nothing
What is the best method. Please help. Also I want to call a method after the thread has done executing the method.
There are two (or more) possible ways. One is to use a
ManualResetEventas follows:Then, in your thread’s start code:
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:
Sorry, I do not speek VB fluently, so you’ll have to convert this little C# snippet 🙂