Automatic interaction with commandoprompt application


K

Kim P

Hi guys,

I am currently trying to make a small application that creates a process,
which runs a console application. The console application needs input in
three sequential steps, and will then terminate.

I have tried using the following approach:

proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
etc...

I even create an eventhandler in order to capture the output from the
console application.

proc.OutputDataReceived += new DataReceivedEventHandler(outputHandler);

This works fine when running an application which runs through all the way
and then exits - but it fails when the console application is pausing,
waiting for user input. The eventhandler never gets called, and I am
unsuccessful in using a StreamWriter to enter input to the console
application.

Anyone got an idea why I cant read output before the application has
successfully exited? And can the "automatic" interaction be done somehow?

Thanks in advance!

Kim P
 
Ad

Advertisements

P

Peter Duniho

[...]
Anyone got an idea why I cant read output before the application has
successfully exited? And can the "automatic" interaction be done somehow?

Impossible to say without a concise-but-complete code example that
reliably demonstrates the problem.

There are some general potential deadlock issues related to the handling
of the three process streams (stdin, stdout, stderr), but without a code
example there's no way to know if that issue applies in your case.

Pete
 
Ad

Advertisements

K

Kim Povlsen

If nothing else works you could perhaps split the console application
into parts, each of which accepts one input and produces one output
for the next part. Run each part separately in sequence.

rossum

Hi again!

Thanks for your reply. I will post the source code here:

-------------------- Source Code ---------------------------

proc = new Process();
proc.StartInfo.FileName = sFileToExecute;
proc.StartInfo.Arguments = sArguments;

proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;

// Set the event handler to asynchronously read the console
process' output.
proc.OutputDataReceived += new DataReceivedEventHandler
(VpnOutputHandler);

proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;

proc.Start();

StreamWriter sIn = proc.StandardInput;
StreamReader sOut = proc.StandardOutput;
StreamReader sErr = proc.StandardError;

// Start the asynchronous read of the process' output stream.
proc.BeginOutputReadLine();

-------------------- Source Code ---------------------------

Now, if I run this on a console application, which terminates by itself
(not requiring any input), it captures the output just fine! But when I
run it on an application which requires input (login/pass) for instance
- the event handler never gets called.

I thought the above code would work as it is supposed to asynchronously
capture any output, but this seems not to be the case.

Any ideas?

Thanks in advance.
Kim P.
 

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