所以我做了一个线程,它执行一个依赖于全局变量的函数。之后,我想更改该变量并再次运行该线程,以便使用另一个参数再次执行我的过程。问题是,我似乎无法弄清楚我的线程何时完成。经过大量的谷歌搜索,我找不到任何可以帮助我的东西。有人可以帮忙吗?提前致谢。一些代码将不胜感激。这里是我的:开始相同的线程两次 - 第一次完成后的第二次
//temp_folder is global variable
temp_folder = APPDATA + "Local" + "\\" + "Temp\\";
Thread oThread = new Thread(new ThreadStart(clean_temp));
oThread.Start();
//Here I should wait for this oThread to finish, then change the temp_folder
//variable mentioned in comment down and start once again
//temp_folder = WINDIR + "\\" + "Temp\\";
//oThread.Start();
这是一个clean_temp()过程。是的,这是GUI应用程序。
public void clean_temp()
{
DirectoryInfo directory = new DirectoryInfo(temp_folder);
try
{
DirectoryInfo[] folders = directory.GetDirectories();
foreach (DirectoryInfo folder in folders)
{
temp_folder = temp_folder + folder.Name + "\\";
clean_temp();
temp_folder = temp_folder.Replace(folder.Name + "\\", "");
}
}
catch { }
long sz;
double size, trenutna;
foreach (FileInfo file in directory.GetFiles())
{
sz = file.Length;
size = (double)sz/1024.0;
trenutna = Convert.ToDouble(lbl_junk_size.Text);
trenutna += size;
MethodInvoker mi_invoker_size_change = new MethodInvoker(()=> lbl_junk_size.Text = trenutna.ToString());
lbl_junk_size.Invoke(mi_invoker_size_change);
MethodInvoker mi_invoker_count_change = new MethodInvoker(() => lbl_junk.Text = (Convert.ToInt64(lbl_junk.Text) + 1).ToString());
lbl_junk.Invoke(mi_invoker_count_change);
}
}
改为使用['ThreadPool'类](http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx),IMO。 – 2014-09-02 11:54:14
是Windows GUI应用程序的主线程部分?或者你打算在清理过程中做其他事情? – 2014-09-02 11:57:16
你可以考虑使用'Task',它有一个'ContinueWith'方法。 – CompuChip 2014-09-02 11:58:35