Don*_*nce 18 .net c# system.diagnostics
我正在使用Process类来运行exe.
exe是我无法控制的第三方控制台应用程序.
我想知道进程是否在命令行上等待输入.
如果它有任何区别,我打算在应用程序等待输入时终止它.
当程序的输出等待读取时,有适当的事件,但是当过程耐心地等待输入时,我看不到任何类似的事件.
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "myapp.exe";
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
process.StartInfo = info;
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
如何检测我的进程正在等待输入?
Ser*_*uei 17
根据第三方进程的具体操作,您可以尝试轮询其线程的状态:
foreach(ProcessThread thread in process.Threads)
if (thread.ThreadState == ThreadState.Wait
&& thread.WaitReason == ThreadWaitReason.UserRequest)
process.Kill();
Run Code Online (Sandbox Code Playgroud)
失败了......你可以尝试
process.StandardInput.Close();
Run Code Online (Sandbox Code Playgroud)
在调用Start()之后,我猜想如果它试图从标准输入中读取,则会在子进程中引发异常.