Objectmix
Tags Register Mark Forums Read

Program started via System.Diagnostics.Process hangs : CSharp

This is a discussion on Program started via System.Diagnostics.Process hangs within the CSharp forums in Programming Languages category; Hello folks, I'm using System.Diagnostics.Process to start a thirdparty program (that works perfectly when started via command line). I'm using Process.StandardOutput to get the output of the program. That works for 95 %, but the other 5 % it doesn't. It seems to me that the started process just hangs, and therefor my program hangs, too (p.WaitForExit()). I researched that this only happens when the output of the program is longer than 4096 bytes: I killed the hung process, WaitForExit returns and in StandardOutput is a string with a length of 4096 chars. The returned string contains the expected output ...


Object Mix > Programming Languages > CSharp > Program started via System.Diagnostics.Process hangs

Reply

 

LinkBack Thread Tools
  #1  
Old 09-25-2007, 04:42 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Program started via System.Diagnostics.Process hangs

Hello folks,

I'm using System.Diagnostics.Process to start a thirdparty program
(that works perfectly when started via command line). I'm using
Process.StandardOutput to get the output of the program. That works
for 95 %, but the other 5 % it doesn't. It seems to me that the
started process just hangs, and therefor my program hangs, too
(p.WaitForExit()). I researched that this only happens when the output
of the program is longer than 4096 bytes: I killed the hung process,
WaitForExit returns and in StandardOutput is a string with a length of
4096 chars. The returned string contains the expected output of the
program, but is shortened to 4 KB.

Does somebody know why it hangs and how to fix this problem?

I'm using this code here:

static string GetProcessOutput(string cmd, string args, out string
error)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo(cmd, args);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.PriorityClass = System.Diagnostics.ProcessPriorityClass.High;
p.WaitForExit();

string output = p.StandardOutput.ReadToEnd();
error = p.StandardError.ReadToEnd();

return output.Trim();
}

Thanks in advance,

Benny Fischer.

Reply With Quote
  #2  
Old 09-25-2007, 06:03 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Program started via System.Diagnostics.Process hangs

Hi,

Remove the wait for exit and all other members, then it will works
perfectly as when started via command line as you wrote.

Cor

<test3@twinmail.de> schreef in bericht
news:1190713377.745604.100650@r29g2000hsg.googlegr oups.com...
> Hello folks,
>
> I'm using System.Diagnostics.Process to start a thirdparty program
> (that works perfectly when started via command line). I'm using
> Process.StandardOutput to get the output of the program. That works
> for 95 %, but the other 5 % it doesn't. It seems to me that the
> started process just hangs, and therefor my program hangs, too
> (p.WaitForExit()). I researched that this only happens when the output
> of the program is longer than 4096 bytes: I killed the hung process,
> WaitForExit returns and in StandardOutput is a string with a length of
> 4096 chars. The returned string contains the expected output of the
> program, but is shortened to 4 KB.
>
> Does somebody know why it hangs and how to fix this problem?
>
> I'm using this code here:
>
> static string GetProcessOutput(string cmd, string args, out string
> error)
> {
> System.Diagnostics.Process p = new System.Diagnostics.Process();
> p.StartInfo = new System.Diagnostics.ProcessStartInfo(cmd, args);
> p.StartInfo.UseShellExecute = false;
> p.StartInfo.RedirectStandardOutput = true;
> p.StartInfo.RedirectStandardError = true;
> p.Start();
> p.PriorityClass = System.Diagnostics.ProcessPriorityClass.High;
> p.WaitForExit();
>
> string output = p.StandardOutput.ReadToEnd();
> error = p.StandardError.ReadToEnd();
>
> return output.Trim();
> }
>
> Thanks in advance,
>
> Benny Fischer.
>



Reply With Quote
  #3  
Old 09-25-2007, 12:12 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Program started via System.Diagnostics.Process hangs

On 25 Sep., 13:03, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
wrote:
> Hi,
>
> Remove the wait for exit and all other members, then it will works
> perfectly as when started via command line as you wrote.
>
> Cor


Thanks for your help Cor, it works perfectly now!

Greetings,
Benny

Reply With Quote
  #4  
Old 09-25-2007, 01:56 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Program started via System.Diagnostics.Process hangs

test3@twinmail.de wrote:
> Hello folks,
>
> I'm using System.Diagnostics.Process to start a thirdparty program
> (that works perfectly when started via command line). I'm using
> Process.StandardOutput to get the output of the program. That works
> for 95 %, but the other 5 % it doesn't.


Cor's advice may have fixed the particular scenario. But you have a
more general problem, which is the possibility that the standard output
will wind up blocked because you haven't been reading from standard error.

In most cases, this may not be a problem, assuming the output to
standard error is brief. But it's a basic issue with redirection of the
Process class standard i/o streams: if you redirect more than one, you
need to make sure you are using some form of asynchronous access to send
or receive data from all of them as appropriate. Otherwise, the console
application winds up blocked trying to read or write to one of the
streams, causing a form of deadlock.

The documentation for the Process class has a good, detailed explanation
of this. It's helpful to read that if you intend to redirect the streams.

Pete
Reply With Quote
Reply

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
System.Diagnostics.Process - Dispose? usenet DOTNET 2 12-04-2007 02:28 AM
System.Diagnostics.Process() hangs usenet DOTNET 5 11-13-2007 07:20 AM
Invoking System.Diagnostics.Process Start and changing the parent process usenet CSharp 2 08-30-2007 08:11 PM
what permissions does a windows service need to execute another process? System.Diagnostics.Process process = System.Diagnostics.Process.Start(info); just local administrator? any specific permitions? usenet XML SOAP 1 04-17-2006 04:11 PM
Using System.Diagnostics.Process object to execute program causes usenet DOTNET 0 01-12-2005 12:11 PM


All times are GMT -5. The time now is 05:37 PM.