Re: System.Diagnostics.Process: Asynchronous reading console output causes problems
- From: "Thorsten Tarrach" <thorsten@xxxxxxxxxxxxxxxx>
- Date: Tue, 3 Feb 2009 18:11:43 +0100
Hi Sun,
thanks, that synchronous version seems to be working so far.
One more question:
I also would like to feed the input to the app via stdin. Unfortunately this has to be terminated by Ctrl+Z or Ctrl+D. How can I send this to the console? I already tried
p.StandardInput.Write("\u001A")
without success.
Many thanks,
Thorsten
""Hongye Sun [MSFT]"" <hongyes@xxxxxxxxxxxxxxxxxxxx> wrote in message news:RXRg2OfhJHA.3596@xxxxxxxxxxxxxxxxxxxxxxxxx
Thanks for Pete's insightful analysis..
Hi Thorsten,
Thanks for your post. My name is Hongye Sun [MSFT] and it is my pleasure to
work with you on this issue.
I want to complement Pete with the following comments:
One possible method in synchronize mode:
The synchronous mode buffer cannot be changed. It is hardcoded as 0x1000.
In synchronous mode, when application running, the child process will first
write to the buffer until it exceeds the buffer size and then it will wait
for the parent process read the buffer. It can continue running after the
buffer is readed. If the parent process waits for child process to run one
second, both child and parent processs will stop running because the child
process is waiting for buffer and the parent process is waiting for child
process. The solution to break this waiting is let parent process to read
the buffer. In order to explain myself well, here is a sample code (I do
not know F# also, so I write it in C#):
-------------------------------------------------
ProcessStartInfo psi = new ProcessStartInfo(@"<Console App
Path>");
psi.CreateNoWindow = true;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
Stopwatch s = new Stopwatch();
s.Start();
while (s.ElapsedMilliseconds < 1000)
{
result += p.StandardOutput.ReadLine() + "\r\n";
}
s.Stop();
p.Close();
Console.Write(result);
Console.Read();
-------------------------------------------------
The sample read the buffer line by line untill the time is greater than one
second. This works fine in our lab's environment.
For the asynchronous mode, I rewrite the code as below. It works perfect to
me. Please try the following code first. If it does not work, please show
us source code of the target console application (eprover.exe). You can
also send it to my mail box hongyes@xxxxxxxxxxxxxxxxxxxx, remove 'online.'
-------------------------------------------------
DataReceivedEventHandler dataRec = new
DataReceivedEventHandler(delegate(object sender, DataReceivedEventArgs e)
{
lock (result)
{
result += (e.Data + "\r\n");
}
});
ProcessStartInfo psi = new ProcessStartInfo(@"<Console App
Path>");
psi.CreateNoWindow = true;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
p.OutputDataReceived += dataRec;
lock (result) { result = ""; }
p.BeginOutputReadLine();
if (!p.WaitForExit(1000))
{
p.Kill();
p.CancelOutputRead();
p.OutputDataReceived -= dataRec;
p.Close();
}
else
{
p.CancelOutputRead();
p.OutputDataReceived -= dataRec;
lock (result) { result = p.StandardError.ReadToEnd() +
result; }
}
Console.Write(result);
Console.Read();
-------------------------------------------------
Please feel free to contact me if you have any question. I am glad to help.
Have a nice day.
Regards,
Hongye Sun (hongyes@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within?2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
- Follow-Ups:
- Re: System.Diagnostics.Process: Asynchronous reading console output causes problems
- From: Peter Duniho
- Re: System.Diagnostics.Process: Asynchronous reading console output causes problems
- From: "Hongye Sun [MSFT]"
- Re: System.Diagnostics.Process: Asynchronous reading console output causes problems
- From: Thorsten Tarrach
- Re: System.Diagnostics.Process: Asynchronous reading console output causes problems
- References:
- System.Diagnostics.Process: Asynchronous reading console output causes problems
- From: Thorsten Tarrach
- Re: System.Diagnostics.Process: Asynchronous reading console output causes problems
- From: Peter Duniho
- Re: System.Diagnostics.Process: Asynchronous reading console output causes problems
- From: Peter Duniho
- Re: System.Diagnostics.Process: Asynchronous reading console output causes problems
- From: Thorsten Tarrach
- Re: System.Diagnostics.Process: Asynchronous reading console output causes problems
- From: "Hongye Sun [MSFT]"
- System.Diagnostics.Process: Asynchronous reading console output causes problems
- Prev by Date: Re: Count # of Instances of a Particular Class
- Next by Date: Re: Count # of Instances of a Particular Class
- Previous by thread: Re: System.Diagnostics.Process: Asynchronous reading console output causes problems
- Next by thread: Re: System.Diagnostics.Process: Asynchronous reading console output causes problems
- Index(es):
Relevant Pages
|