我的代码:的AutoResetEvent不工作
static AutoResetEvent wait_till_finish = new AutoResetEvent(false);
...if (File.Exists("try.exe"))
{
Thread quartus_thread = new Thread(() => qar_function(@"\quartus");
quartus_thread.Start();
wait_till_finish.WaitOne();
// ONLY after command mode action was finished, and AutoResetEvent is set, lookfor some file in folder
if (File.Exists("def")) {//do something}
}
,稍后:
public void qar_function(string abc)
{ //does something...
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/k " + String.Join(" ", args));
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
// ***** now set AutoResetEvent:
wait_till_finish.set();
我的问题是:
我`wait_till_finish.WaitOne()在一种方法中,并且在我调用Qar_Function方法之后它处于等待状态,所以首先我要调用方法,然后我想等待方法执行并完成,然后在qar_function方法内设置AutoR ESET。
它不工作。
我正在使用调试器,它并没有在WaitOne等待,它只是继续移动到下一行。
我在做什么错? Thx。
您的意思是启动一个进程或等待进程结束?在设置句柄 – 2015-01-21 08:40:20
之前,您可能需要调用'proc.WaitForExit()'来完成我的过程,然后再找到该文件。我现在认为它会在启动时发生,而不是在完成时发生......我该如何改变它? – 2015-01-21 08:44:40
您必须在代码的开头某处调用Reset()方法,或者,使用true初始化您的AutoResetEvent:wait_till_finish = new AutoResetEvent(true); – Younes 2015-01-21 08:45:19