C# で実行可能ファイルを呼び出しています。実行可能ファイルが実行されると、コンソールに出力されるため、C# コードはコンソール出力を取得してテキスト ファイルに書き込みます。クラッシュが発生すると、いくつかのことが起こります。
1) テキスト ファイルの出力が完全に書き出されていません。2) 実行が成功した場合と同じようにレポートが生成されるため、実行可能プロセスは完全に実行されているように見えます。
このクラッシュの原因は、ファイルの書き方にあると思われます。
更新: - クラッシュに関する限り、「Manager で問題が発生したため、閉じる必要があります。ご迷惑をおかけして申し訳ありません」というダイアログが表示されます。次に、OKボタンがあります。[OK] をクリックすると、マネージャーをもう一度開始するかどうかを確認するダイアログが表示されます。
- 実行可能ファイルを呼び出すマネージャ アプリケーションはシングル スレッドです。実行可能ファイルはマルチスレッドで実行できます。
呼び出しの小さなスニペットを次に示します。
// Set up process to redirect standard output and standard error to
// a file.
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
FileInfo ofi = new FileInfo(outputPath);
FileStream ofs = ofi.OpenWrite();
StreamWriter sw = new StreamWriter(ofs);
WriteToTextWriterEventHandler wtsweh = new WriteToTextWriterEventHandler(sw);
DataReceivedEventHandler handler = wtsweh.HandleDataReceived;
process.OutputDataReceived += handler;
process.ErrorDataReceived += handler;
//
statusAcceptor.ReportStatus("Running process.");
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
statusAcceptor.ReportStatus("Waiting for process to complete.");
process.WaitForExit();
int processExitCode = process.ExitCode;
process.Close();
sw.Close();
//
private class WriteToTextWriterEventHandler
{
private TextWriter tw;
public WriteToTextWriterEventHandler(TextWriter tw)
{
this.tw = tw;
}
public void HandleDataReceived(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
tw.Write(Environment.NewLine + outLine.Data);
}
}
}