我有一个简单的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是否发生了更改?在单独的线程中,如何做到这一点?
我希望有人能对这个问题有所了解。提前谢谢。
发布于 2013-01-25 04:53:26
在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();https://stackoverflow.com/questions/8978512
复制相似问题