2

I already know how to catch standard output of a console window, BUT my problem is the case when I get the process with GetProcesses/orByName and do not Start() it myself. Here is the code:

public ProcessCaller(ISynchronizeInvoke isi, Process MárFutóAlkalmazás)
  : this(isi)
{
  //alapbeállítások
  FileName = MárFutóAlkalmazás.StartInfo.FileName;
  Arguments = MárFutóAlkalmazás.StartInfo.Arguments;
  WorkingDirectory = MárFutóAlkalmazás.StartInfo.WorkingDirectory;

  //egyedi beállítások
  process = MárFutóAlkalmazás;
  process.EnableRaisingEvents = true;
  process.StartInfo.CreateNoWindow = true;
  process.StartInfo.UseShellExecute = false;
  process.StartInfo.RedirectStandardOutput = true;
  process.StartInfo.RedirectStandardError = true;
  new MethodInvoker(ReadStdOut).BeginInvoke(null, null);
  new MethodInvoker(ReadStdErr).BeginInvoke(null, null);

  //események
  StdErrReceived += new DataReceivedHandler(Loggolás);
  StdOutReceived += new DataReceivedHandler(Loggolás);

  //kilépés jelzése
  process.Exited += new EventHandler(OnKilépés);
}

So this method gets and already running application as MárFutóAlkalmazás parameter. Sets some internal properties, then hooks to Output. However when it comes to

StdOutReceived += new DataReceivedHandler(Loggolás);

and the program runs the Loggolás method to take the console data, it says that the StandardOut is not set, or the process is not started.

Well:

  • StandardOut is set
  • Process is running, since I get it by GetProcesses
  • In this routine I do NOT use process.Start() - since it is started already

Looking for help. Thank yas: Péter

Zéiksz
  • 688
  • 1
  • 11
  • 25
  • 1
    +1: Excellent first question, very well asked. We need more new users like you :) – Binary Worrier Aug 26 '10 at 09:57
  • possible duplicate of [Capture output from unrelated process](http://stackoverflow.com/questions/429225/capture-output-from-unrelated-process) – Binary Worrier Aug 26 '10 at 10:00
  • I checked that "Capture output from unrelated process" you suggested, and after modifying the code there I got the same error message: the StandardOut (no such property BTW, only StandardOutput) is not set, or the process is not started. – Zéiksz Aug 26 '10 at 10:46
  • Note to above: StartInfo for Process produced by GetProcesses is not filled out at all, AND OutputDataReceived event is not firing at all even if EnableRaisingEvents is set to true. – Zéiksz Aug 26 '10 at 10:49

1 Answers1

0

Ok, so after asking around and checking on net, I learned that you can not hook on an output not started by you. So if your executor application crashes, you will have to restart console application to be able to capture output. You need the .Start().

Actually I see only one salvation for this problem: starting with the ">filename.txt" or such output redirecting parameter. This will stuff everything into a file, so even if executor application crashes you can "reconnect" if you "read only". Did not tested this yet, but I see no other way.

Zéiksz
  • 688
  • 1
  • 11
  • 25
  • For those who are interested, there is a good document with examples how to redirect console output into files: http://ss64.com/bash/syntax-redirection.html – Zéiksz Aug 26 '10 at 20:45
  • Starting application should be through "cmd" as program name "/c ...parameters... > 2>&1" as parameter. If you start application with "" as program name (in startinfo), then you are to steal the output yourself. – Zéiksz Aug 27 '10 at 12:30