Can not Get Process Output perfectly.

D

Div /

I have a very strange problem here. I have a wrapper class for a
command line MyProgram.exe. When i run it from the command prompt
(MyProgram.exe), it runs perfectly. So there is not problem with it
for sure. When I fire some command from my C# program to this exe, I
get the response for ALMOST all the commands perfectly.

BUT For a few commands, my program sometimes gives the desired output,
and sometimes only half of it. (as it gives at command prompt). AND
sometime, process doesnot return any output after I fire certain
commands. I am not able to figure out where is the problem and why It
is occuring. This is happening for any command and not for any
specific command.

I am posting the code below. I would really appreciate help on these.

------------------------------------------------------------------
....
....
....
private Process m_pro = new Process();
ProcessStartInfo myInfo = new ProcessStartInfo("MyProgram.exe");

myInfo.Arguments = " /u " + UserName +
" /p " + Password;

myInfo.UseShellExecute=false;
myInfo.RedirectStandardOutput=true;
myInfo.RedirectStandardInput=true;
myInfo.RedirectStandardError = true;
myInfo.CreateNoWindow=true;
myInfo.WorkingDirectory = InstallDir;

m_pro.StartInfo=myInfo;
m_pro.Start();

....
....
....

string myString = "";
string tmp = "";
bool skip = false;

try
{
StreamReader m_readStream = m_pro.StandardOutput;

m_pro.StandardInput.Write(command + "\n\n");
m_pro.StandardInput.Flush();

//--- HERE in below code m_readStream does not give any output
SOMETIMES.
//-- MOST of the time it works fine.

// Remove the prompt
while (m_readStream.Peek() >=0)
{
tmp = m_readStream.ReadLine();
if (tmp.IndexOf("prompt>")>=0)
break;
}

while (m_readStream.Peek() >=0)
{
tmp = m_readStream.ReadLine();
if (tmp == "") { skip = true;}
if (!skip)
myString = myString + tmp + "\r\n";
if (tmp.IndexOf("prompt>")>=0)
break;
}

m_pro.Refresh();

return myString;
}
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Div /,

It a only guess, but look at these lines
while (m_readStream.Peek() >=0)
{
tmp = m_readStream.ReadLine();
if (tmp.IndexOf("prompt>")>=0)
break;
}

I don't know how your console app works, but if it outputs

prompt>[some valuable data here]

you'll skip the data part because ReadLine will put in *tmp* the whole line.
Then you check for 'prompt>' and discard the rest.

So my guess is that you skip data using those ReadLine-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top