是我的过程中等待输入? [英] Is my process waiting for input?

查看:281
本文介绍了是我的过程中等待输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Process类运行exe文件。



该EXE是我无法控制第三方控制台应用程序。



我想知道该进程是否正在等待命令行中输入。



如果这有什么区别,我打算杀应用程序是否应该等待输入。



有用于当有从程序等待输出读取合适的事件,但我看不到任何类似的过程时, 。在耐心等待输入

 的ProcessStartInfo信息=新的ProcessStartInfo(); 
info.FileName =MyApp.exe的;
info.CreateNoWindow = TRUE;
info.UseShellExecute = FALSE;
info.RedirectStandardError = TRUE;
info.RedirectStandardInput = TRUE;
info.RedirectStandardOutput = TRUE;
process.StartInfo =信息;

process.OutputDataReceived + =新DataReceivedEventHandler(process_OutputDataReceived);
process.ErrorDataReceived + =新DataReceivedEventHandler(process_ErrorDataReceived);

的Process.Start();

process.BeginOutputReadLine();
process.BeginErrorReadLine();


process.WaitForExit();



我如何检测我的进程正在等待输入?


< DIV CLASS =h2_lin>解决方案

根据什么第三方程序做正是你可以尝试轮询的线程的状态:

 的foreach(ProcessThread线程process.Threads)
如果(thread.ThreadState == ThreadState.Wait
和;&安培; thread.WaitReason == ThreadWaitReason.UserRequest)
process.Kill();



如果做不到这一点......你可以尝试

  process.StandardInput.Close(); 



调用Start()之后,我推测,一个异常将在子过程中提出,如果它试图从标准输入读取。


I am using the Process class to run an exe.

The exe is a 3rd party console application that I do not control.

I wish to know whether the process is waiting for input on the command line.

Should it make any difference, I intend to kill the application should it be waiting for input.

There are suitable events for when there is output from the program waiting to be read, but I cannot see anything similar for when the process is waiting patiently for input.

            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();

How do I detect that my process is waiting for input?

解决方案

Depending on what the 3rd party process is doing exactly you could try polling its threads' states:

foreach(ProcessThread thread in process.Threads)
    if (thread.ThreadState == ThreadState.Wait
        && thread.WaitReason == ThreadWaitReason.UserRequest)
            process.Kill();

Failing that... you can try to

process.StandardInput.Close();

after calling Start(), I conjecture that an exception will be raised in the child process if it's trying to read from standard input.

这篇关于是我的过程中等待输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆