Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Nested process hangs when redirecting standard output

$
0
0
I have the following situation:
Application1 starts Application2 via Process.Start and redirects its standard output
Application2 starts a 3rd process (external application), also via Process.Start and redirects its standard output
This 3rd process hangs when Application2 redirects its standard output.

The source code of Application1 looks like this:
1ProcessStartInfo lStartInfo = new ProcessStartInfo(path, args);  
2lStartInfo.UseShellExecute = false;  
3lStartInfo.CreateNoWindow = true;  
4lStartInfo.RedirectStandardInput = true;  
5lStartInfo.RedirectStandardOutput = true;  
6lStartInfo.RedirectStandardError = true;  
7 
8fProcess = new Process();  
9fProcess.StartInfo = lStartInfo;  
10fProcess.EnableRaisingEvents = true;  
11fProcess.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e)  
12{  
13    if (!string.IsNullOrEmpty(e.Data))  
14        this.EventLog.WriteEntry(e.Data, EventLogEntryType.Information);  
15};  
16fProcess.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e)  
17{  
18    if (!string.IsNullOrEmpty(e.Data))  
19        this.EventLog.WriteEntry(e.Data, EventLogEntryType.Error);  
20};  
21 
22fProcess.Start();  
23fProcess.BeginOutputReadLine();  
24fProcess.BeginErrorReadLine(); 

The source code of Application2 looks like this:
1ProcessStartInfo lStartInfo = new ProcessStartInfo(path, args);  
2lStartInfo.UseShellExecute = false;  
3lStartInfo.CreateNoWindow = true;  
4lStartInfo.RedirectStandardOutput = true;  
5lStartInfo.RedirectStandardError = true;  
6 
7using(Process lProcess = new Process())  
8{  
9    lProcess.StartInfo = lStartInfo;  
10    lProcess.EnableRaisingEvents = true;  
11    lProcess.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e)  
12    {  
13        if (!string.IsNullOrEmpty(e.Data))  
14            fStringBuilderLog.AppendLine(e.Data);  
15    };  
16    lProcess.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e)  
17    {  
18        if (!string.IsNullOrEmpty(e.Data))  
19            fStringBuilderLog.AppendLine(e.Data);  
20    };  
21 
22    lProcess.Start();  
23    lProcess.BeginOutputReadLine();  
24    lProcess.BeginErrorReadLine();  
25    lProcess.WaitForExit();  
26

If I change Application2 so it doesn't redirect standard output, the 3rd process does not hang. So it appears to be some kind of dead-lock with nested processes that both redirect the standard output of their child-process.

Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>