tags:

views:

1787

answers:

5

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);
          }
      }
  }
+1  A: 

What is the crash that you are getting? A .Net exception?

That the file is not written completely could be because you do not flush the stream into the file and then close it.

I think it might be a problem that you use the same stream for both standard out and error out messages. This might lead to concurrency problems.

0xA3
+1  A: 

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?)

GWLlosa
I added a flush after the write and so far I have not been able to reproduce the crash for a couple of days now. Why would adding the flush work?
arc1880
As much as I'd like to take credit for that... I have no idea. My thinking behind the flush was simply that the entire output stream might not be displaying, potentially hiding some useful error message.
GWLlosa
A: 

I've tested your code with a number of different executables and was unable to get it to crash the way you describe. Maybe it's an issue with the process you're executing?

w4g3n3r
+1  A: 

If you attach a debugger to the crashing process, you will know exactly why it crashed. Here's a detailed tutorial on debugging crashes: http://blogs.msdn.com/kirillosenkov/archive/2008/12/07/how-to-debug-crashes-and-hangs.aspx

Kirill Osenkov
+1  A: 

It is probably in your handlers. You should put in logic to handle exceptions thrown by the stream handlers and you should put in a mechanism to make sure that the stream handlers are properly closed before you call close on the process. Problems like this are hard to pin down because of the timing issues with the event handlers and the process.close so it doesnt suprise me other posters have not been able to reproduce it. But I have seen it in action. The problem is the EventHandler is active until it is closed by calling cancelErrorRead or cancelOutputRead or the process exits. Well, if it is still busy flushing some output from the very end of the process, while the main thread makes it to Process.Close...BOOM

Alex