私はc#のコマンドラインを使用してプログラムを実行しています。このプログラムはいくつかのログを生成しますが、実行中は変更が発生するたびにこのログを表示する必要があります。次のコードを記述しましたが、プロセスが強制終了され、実行中にプログラムが応答しなくなると、すべてのログが表示されます。どうすれば修正できますか?
よろしく
ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "C:\\server.py");
Process proc = new Process();
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
//procStartInfo.CreateNoWindow = true;
proc.StartInfo = procStartInfo;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(300);
LogstextBox.Text = output;
編集済み:
まあ、使用しようとしましOutputDataReceived
たが、結果が表示されません。変更されたコードは次のとおりです。
{
//processCaller.FileName = @"ping";
//processCaller.Arguments = "4.2.2.4 -t"; this is working
processCaller.FileName = @"cmd.exe";
processCaller.Arguments = "/c c:\\server.py"; //this is not working
processCaller.StdErrReceived += new DataReceivedHandler(writeStreamInfo);
processCaller.StdOutReceived += new DataReceivedHandler(writeStreamInfo);
processCaller.Completed += new EventHandler(processCompletedOrCanceled);
processCaller.Cancelled += new EventHandler(processCompletedOrCanceled);
this.richTextBox1.Text = "Server Started.." + Environment.NewLine;
processCaller.Start();
}
private void writeStreamInfo(object sender, DataReceivedEventArgs e)
{
this.richTextBox1.AppendText(e.Text + Environment.NewLine);
}