簡體   English   中英

進程診斷等待用戶輸入

[英]process.diagnostic waiting for user input

我有一個簡單的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是否已更改? 在單獨的線程中該怎么做?

我希望有人可以在這個問題上有所作為。 提前致謝。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM