有两种(或更多)可能的方式。一种是使用ManualResetEvent
如下:
_Event = New ManualResetEvent(False); <-- Create event globally
然后,在你的线程的启动代码:
_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
在你的线程的方法,你必须在所有情况下方法返回之前调用_Event.Set()
,否则你的应用程序将被阻止。
另一种方法是必须在完成时线程调用委托。您想要在线程完成后执行的代码(Me.MainInit()
)将进入委托方法。这实际上是相当优雅的。
例如:
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
}
对不起,我不磺化聚醚醚酮VB流利,所以你必须给这个小C#代码段:-)
没问题。我理解这两个世界。不管怎么说,还是要谢谢你。 – 2010-06-11 09:54:34
我有一个问题,我在线程方法内设置了一个进度条的值,所以每次我在写入_event.set()之前设置进度条值,并从方法返回并执行maininit()方法。 – 2010-06-11 10:24:42
更新进度栏时,不要调用_event.Set()。你只能在你的线程方法真正退出之前调用它(无论是因为错误还是因为它的完成)。 – 2010-06-11 10:34:16