如何在使用C#中断运行进程后继续在命令行窗口中执行其他进程

时间:2021-07-09 00:34:24

I have written C# command window application. I'm running bunch of processes on command line inside the main(). For e.g.

我编写了C#命令窗口应用程序。我在main()内的命令行上运行了一堆进程。对于例如

void main()
{
    process p1 = new process()
    set p1 properties
    p1.start()
 -->p1.StandardInput.WriteLine("start /WAIT cmd.exe /c BUILD -cZP");
}

This line will execute some program in a new command window. While executing that last line I will break this execution using ctrl+c and return the control to execution of the main program.

该行将在新的命令窗口中执行某个程序。在执行最后一行时,我将使用ctrl + c中断执行并将控件返回到主程序的执行。

loop through to output to the execution window.

循环输出到执行窗口。

p1.StandardInput.WriteLine("Done some action"); 
p1.WaitForExit();
p1.Close();

The above three lines are not executed. The question is p1 never closes to execute following lines which I have in my program.

以上三行未执行。问题是p1永远不会关闭执行我的程序中的以下行。

process p2 = new process()
...
p2.waitforExit()
p2.close.

Any insight for above challenges will be great. thx.

对上述挑战的任何见解都会很棒。谢谢。

1 个解决方案

#1


1  

If I understand you correctly (which I admit I may not be understanding you), I believe the problem is that when you press CTRL-C to break into process p1, you are actually killing that process. Then you are trying to send text to the standard input for the process, which has just been killed. Since the process is no longer available to take your input, the main program hangs. That's my best guess.

如果我理解正确(我承认我可能不理解你),我认为问题是当你按CTRL-C进入进程p1时,你实际上是在杀死那个进程。然后,您尝试将文本发送到刚刚被杀死的进程的标准输入。由于该过程不再可用于输入,因此主程序会挂起。这是我最好的猜测。

#1


1  

If I understand you correctly (which I admit I may not be understanding you), I believe the problem is that when you press CTRL-C to break into process p1, you are actually killing that process. Then you are trying to send text to the standard input for the process, which has just been killed. Since the process is no longer available to take your input, the main program hangs. That's my best guess.

如果我理解正确(我承认我可能不理解你),我认为问题是当你按CTRL-C进入进程p1时,你实际上是在杀死那个进程。然后,您尝试将文本发送到刚刚被杀死的进程的标准输入。由于该过程不再可用于输入,因此主程序会挂起。这是我最好的猜测。