2017-06-04 71 views
0

我正在使用的是一个程序,它在命令提示符下运行命令并将结果输出到窗体中。但是,无论何时您启动该命令,它都会锁定窗体窗体,然后您无法再单击窗体上的任何内容。这是一个问题,因为我想添加一个停止按钮,请参阅下面的相应代码。在命令提示符运行时使用Windows窗体

private void Start_Click(object sender, EventArgs e) 
    { 
     var proc = new Process(); 
     proc.StartInfo.Arguments = "/C robocopy " + "\"" + source.Text + "\" \"" + Destination.Text + "\" /E /ZB /W:1 /R:3 /MT /A-:SH"; 
     proc.StartInfo.FileName = "cmd.exe"; 
     proc.StartInfo.RedirectStandardOutput = true; 
     proc.StartInfo.RedirectStandardError = true; 
     proc.EnableRaisingEvents = true; 
     proc.StartInfo.CreateNoWindow = true; 
     proc.StartInfo.UseShellExecute = false; 
     // see below for output handler 
     proc.ErrorDataReceived += proc_DataReceived; 
     proc.OutputDataReceived += proc_DataReceived; 

     proc.Start(); 

     proc.BeginErrorReadLine(); 
     proc.BeginOutputReadLine(); 

     proc.WaitForExit(); 

    } 

回答

1

删除对proc.WaitForExit();的调用,因为这会阻止执行。参见[该文档(https://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforexit(v=vs.110).aspx)约WaitForExit

设置的时间段等待相关联的过程退出,块执行的当前线程,直到时间已经过去或进程已退出。为避免阻塞当前线程,请使用Exited事件。

如前所述,如果您想在进程退出后运行代码,请使用Exited事件。由于你的代码不这样做,你可以跳过这一步。

+0

工作完美;非常感谢你! – xjacksssss

相关问题