process.diagnostic 等待用户输入

发布于 2024-12-28 19:06:43 字数 654 浏览 4 评论 0原文

我有一个与另一个控制台程序通信的简单 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2025-01-04 19:06:43

在 WPF 应用程序中,您可以使用 System.Windows.Threading.DispatcherTimer

改编自 MSDN 文档的示例:

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

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文