473,473 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Can not Get Process Output perfectly.

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;
}
Nov 16 '05 #1
1 2596
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

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Div /" <di***********@hotmail.com> wrote in message
news:ef**************************@posting.google.c om... 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;
}

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Richard | last post by:
Good morning all I need a way in visual basic 6, to start a console process, like ping -A xxx.xxx.xxx.xxx > outputfile.txt and make vb hold its own execution till this process finishes....
1
by: Div / | last post by:
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...
0
by: Div / | last post by:
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...
2
by: test3 | last post by:
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.