Console Output not catched by a asynchronous read operation

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have implemented an asynchronous operation to read the standard output of a Process (see the MyTask class bellow).
If I execute "C:WindowsMicrosoft.NETFrameworkv4.0.30319aspnet_compiler.exe" in my process, everything works fine and I get the whole content of aspnet_compilers standard output.
However, if I execute any console application I wrote, I get nothing. May I be using the wrong method to write in the standard output ? Here after is also the code on a dummy console application I wrote for testing purpose.
Could someone tell me where I do the mistake ???

<pre style="font-family:Consolas; background-color:white <span style="color:blue class <span style="color:#2b91af MyProgram
{
<span style="color:blue static <span style="color:blue void Main(<span style="color:blue string[] args)
{
<span style="color:#2b91af Console.WriteLine(<span style="color:#a31515 "A Message as been written on the console.");
<span style="color:#2b91af Console.Out.WriteLine(<span style="color:#a31515 "A Message as been written on the consoles standard output.");
<span style="color:#2b91af Console.Out.Flush();
}
}[/code]

<pre style="font-family:Consolas; background-color:white <span style="color:blue public <span style="color:blue class <span style="color:#2b91af MyTask
{
<span style="color:blue private StringBuilder outputText;
<span style="color:blue private StringBuilder errorText;

<span style="color:blue public <span style="color:blue int execute(<span style="color:blue string command, <span style="color:blue string args)
{
ProcessStartInfo psi = <span style="color:blue new ProcessStartInfo(command);
psi.Arguments = args;
psi.CreateNoWindow = <span style="color:blue true;

psi.UseShellExecute = <span style="color:blue false;
psi.RedirectStandardOutput = <span style="color:blue true;
psi.RedirectStandardError = <span style="color:blue true;
psi.WorkingDirectory = Path.GetDirectoryName(command);
<span style="color:blue using (Process tool = Process.Start(psi))
<span style="color:blue using (ManualResetEvent mreOut = <span style="color:blue new ManualResetEvent(<span style="color:blue false), mreErr = <span style="color:blue new ManualResetEvent(<span style="color:blue false))
{
outputText = <span style="color:blue new StringBuilder();
errorText = <span style="color:blue new StringBuilder();

tool.EnableRaisingEvents = <span style="color:blue true;

tool.OutputDataReceived += <span style="color:blue delegate(<span style="color:blue object sendingProcess, DataReceivedEventArgs outLine)
{
<span style="color:blue if (<span style="color:blue string.IsNullOrEmpty(outLine.Data))
{
tool.CancelOutputRead();
mreOut.Set();
}
<span style="color:blue else
outputText.AppendLine(outLine.Data);
};
tool.BeginOutputReadLine();

tool.ErrorDataReceived += <span style="color:blue delegate(<span style="color:blue object sendingProcess, DataReceivedEventArgs errorLine)
{
<span style="color:blue if (<span style="color:blue string.IsNullOrEmpty(errorLine.Data))
{
tool.CancelErrorRead();
mreErr.Set();
}
<span style="color:blue else
errorText.AppendLine(errorLine.Data);
};
tool.BeginErrorReadLine();

<span style="color:blue if (tool.WaitForExit(3600000))
{
mreOut.WaitOne();
mreErr.WaitOne();

exitCode = tool.ExitCode;
}
}

<span style="color:blue return exitCode;
}
}[/code]


<hr class="sig Valéry Letroye

View the full article
 
Back
Top