I am calling an executable in C#. When the executable runs, it writes out to the console so the C# code is getting the console output and writing it to a text file. When the crash happens a couple of things occur.
1) The output of the text file is not completely written out. 2) The executable process seems to run completely through because it generates a report just like a successful run.
I suspect that the reason for this crash is because of how I’m writing the file out.
Update: – As far as the crash, a dialog comes up saying that the ‘Manager has encountered a problem and needs to close. We are sorry for the inconvenience.’ Then it has an OK button. When you click OK, there is a dialog that I have setup that asks if I want to start the manager again.
- The manager application that is calling the executable is single threaded. The executable may run multi-threaded.
Here is a small snippet of the call:
// 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); } } }
Try adding a flush after the tw.Write. That should cause the complete output up to the point of failure to be produced, which may include error messages from the executable (if that’s what is crashing?)