process.diagnostic 等待用户输入
我有一个与另一个控制台程序通信的简单 WPF 应用程序。我使用 Process.Diagnostic 启动控制台应用程序。该控制台应用程序有一个提示,因此我可以通过 StandardInput
发送输入并通过 StandardOutput
读取结果。 我只想在 WPF 应用程序加载并继续发送输入并读取输出时启动控制台应用程序一次(始终保持活动状态)。
我有一些代码,但我不知道如何将它们组合在一起。
问题是,发送输入后,我想等到出现提示后再开始逐行读取输出,以便获得完整的结果。我知道我可以检查进程是否正在等待这样的输入:
foreach (ProcessThread thread in _proccess.Threads)
{
if (thread.ThreadState == System.Diagnostics.ThreadState.Wait
&& thread.WaitReason == ThreadWaitReason.UserRequest)
{
_isPrompt = true;
}
}
但是我应该将该代码放在哪里来检查 ThreadState 是否已更改?在单独的线程中又该如何做呢?
我希望有人能够阐明这个问题。 提前致谢。
I have a simple WPF application that communicates with another console program. I use Process.Diagnostic
to launch the console app. That console app has a prompt so I can send the input through StandardInput
and read the outcome through StandardOutput
.
I want to launch the console application only once (keep it alive whole time) when the WPF apps loads and keep sending input and reads the output.
I have some piece of code but I don’t how to put it all together.
The problem is that after sending the input I want to wait until the prompt occurs before I start reading the output line by line so I have entire outcome. I know I can check if the process is waiting for input like that:
foreach (ProcessThread thread in _proccess.Threads)
{
if (thread.ThreadState == System.Diagnostics.ThreadState.Wait
&& thread.WaitReason == ThreadWaitReason.UserRequest)
{
_isPrompt = true;
}
}
But where should I put that code to check if the ThreadState
has changed? In a separate thread and how to do that?
I hope that someone can put some light on that issue.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 WPF 应用程序中,您可以使用 System.Windows.Threading.DispatcherTimer。
改编自 MSDN 文档的示例:
In a WPF app you can use the System.Windows.Threading.DispatcherTimer.
Example adapted from MSDN documentation: