Readline hangs in process.StandardOutput

  • Thread starter Thread starter JC
  • Start date Start date
J

JC

I'm trying to create a GUI wrapper for dumpbin, and so I'm using the Process
class to run the command-line application. The problem is that if I use
Readline() to get the output from the commandline app, it will hang when it
comes to the command-prompt (there's no return to send it to readline).
Here's the code:

public bool RunCommands(string cmds, ArrayList files)

{

try

{

StreamReader sr = VSCmd.StandardOutput; // VSCmd is the Process
object running cmd.

string s;

string t = "";


foreach(string file in files)

{

stWriter.WriteLine("Dumpbin" + cmds + " " + file);

while ((s=sr.ReadLine())!=null) // When Dumpbin runs once,
it finishes with a prompt; application will hang here waiting for a return.

t += s;

}

System.Windows.Forms.MessageBox.Show(t);

return true;

}

catch (Exception e)

{

System.Diagnostics.Debug.WriteLine("Error in
CVSCmdPrompt.RunCOmmands: " + e.ToString());

return false;

}


}



I want to loop through the files for various reasons, so I don't want to
wait for the Process to shut down. Also, i've noticed that if i don't
redirect the output to the process, Visual Studio sends it to the Output
screen anyway! And it doesn't hang at the prompt. How does it do this? Can I
tap this?



Thanks

Josh
 
Hi JC,

Thanks for posting in this group.
To redirect the StandardOutput you should first set
ProcessStartInfo.RedirectStandardInput to true, then set
ProcessStartInfo.UseShellExecute to false.
Do like this:
try
{
Process VSCmd=new Process();
VSCmd.StartInfo=new ProcessStartInfo(@"C:\Program Files\Microsoft Visual
Studio\VC98\Bin\dumpbin.exe");
VSCmd.StartInfo.Arguments=@"C:\WINDOWS\system32\notepad.exe";
VSCmd.StartInfo.RedirectStandardOutput=true;
VSCmd.StartInfo.UseShellExecute=false;

VSCmd.Start();

StreamReader sr = VSCmd.StandardOutput; // VSCmd is the Process object
running cmd.
string s;
string t = "";
while ((s=sr.ReadLine())!=null) // When Dumpbin runs once,it finishes
with a prompt; application will hang here waiting for a return.
t += s;
sr.Close();
System.Windows.Forms.MessageBox.Show(t);
}
catch (Exception ex)
{
MessageBox.Show("Error in CVSCmdPrompt.RunCOmmands: " + ex.Message);
}

It works well on my machine, hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "JC" <none>
| Subject: Readline hangs in process.StandardOutput
| Date: Tue, 18 Nov 2003 15:48:40 -0800
| Lines: 70
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: tide106.microsoft.com 207.46.228.31
| Path:
cpmsftngxa08.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.
phx.gbl
| Xref: cpmsftngxa08.phx.gbl microsoft.public.dotnet.languages.csharp:198478
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I'm trying to create a GUI wrapper for dumpbin, and so I'm using the
Process
| class to run the command-line application. The problem is that if I use
| Readline() to get the output from the commandline app, it will hang when
it
| comes to the command-prompt (there's no return to send it to readline).
| Here's the code:
|
| public bool RunCommands(string cmds, ArrayList files)
|
| {
|
| try
|
| {
|
| StreamReader sr = VSCmd.StandardOutput; // VSCmd is the
Process
| object running cmd.
|
| string s;
|
| string t = "";
|
|
| foreach(string file in files)
|
| {
|
| stWriter.WriteLine("Dumpbin" + cmds + " " + file);
|
| while ((s=sr.ReadLine())!=null) // When Dumpbin runs
once,
| it finishes with a prompt; application will hang here waiting for a
return.
|
| t += s;
|
| }
|
| System.Windows.Forms.MessageBox.Show(t);
|
| return true;
|
| }
|
| catch (Exception e)
|
| {
|
| System.Diagnostics.Debug.WriteLine("Error in
| CVSCmdPrompt.RunCOmmands: " + e.ToString());
|
| return false;
|
| }
|
|
| }
|
|
|
| I want to loop through the files for various reasons, so I don't want to
| wait for the Process to shut down. Also, i've noticed that if i don't
| redirect the output to the process, Visual Studio sends it to the Output
| screen anyway! And it doesn't hang at the prompt. How does it do this?
Can I
| tap this?
|
|
|
| Thanks
|
| Josh
|
|
|
 
Back
Top