【发布时间】:2014-07-03 17:46:57
【问题描述】:
我创建了一个执行任务的线程,但我需要暂停我的主线程,直到我的辅助线程结束任务。
private void AquilesPL_Load(object sender, EventArgs e)
{
ThreadStart ts = new ThreadStart(RunTask)
Thread t = new Thread(ts);
t.Start();
SomeFunction1();
SomeFunction2();
//I need to pause the main thread here, if runtask() continue working
//if runt task ends, this main thread must to continue.
ReadFile();
CloseProgram();
}
private void RunTask()
{
//Some code that write a file
//RunTaskfunction ends, and i have to continue
}
private void ReadFile()
{
//Reading the file, this file has been written by RunTask
}
提前致谢。
【问题讨论】:
-
EventArgs 表明您做错了。
-
您正在使用或可以使用哪个 C# / .NET 版本?应用程序的类型是什么(WinForms?)?
-
您永远不会“暂停” GUI 应用程序的主线程。除了死锁或无功能的冻结用户界面之外,这并没有完成任何事情。使用控件的 Enabled 属性来防止它们在工作完成时被使用。一个指示进度的对话框是一个显而易见的选择。
标签: c# .net multithreading winforms