473,473 Members | 2,002 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 2595
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
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.