进程诊断等待用户输入 [英] process.diagnostic waiting for user input

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

问题描述

我有一个简单的WPF应用程序,可与另一个控制台程序进行通信.我使用Process.Diagnostic启动控制台应用程序.该控制台应用程序有一个提示,因此我可以通过StandardInput发送输入并通过StandardOutput读取结果. 当WPF应用加载时,我只希望启动控制台应用一次(始终保持活动状态),并继续发送输入并读取输出.

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;
    }
}

但是我应该在哪里放置代码以检查ThreadState是否已更改?在单独的线程中以及如何执行此操作?

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.

推荐答案

在WPF应用程序中,您可以使用

In a WPF app you can use the System.Windows.Threading.DispatcherTimer.

根据MSDN文档改编的示例:

Example adapted from MSDN documentation:

// code assumes dispatcherTimer, _process and _isPrompt are declared on the WFP form

this.dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
this.dispatcherTimer.Tick += (sender, e) =>
{
    this._isPrompt = proc
        .Threads
        .Cast<ProcessThread>()
        .Any(t => t.WaitReason == ThreadWaitReason.UserRequest);
};
this.dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
this.dispatcherTimer.Start();

...

this._process.Start();

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

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