473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2582
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.