Redirecting Standard Input Interactively

G

Guest

Hi,
I am a newbie to C# and Im trying to redirect standard input, output and
error of a console program written in C (MS VC 6.0) to a textbox on a form.
The code for the redirecting looks like this:

private System.IO.StreamWriter c_StreamInput = null;
private System.IO.StreamReader c_StreamOutput = null;
private Thread c_ThreadRead = null;
private Process c_Process = null;

private void ReadStdOutputThreadProc()
{
while(!c_Process.HasExited)
{
try
{
string str = c_StreamOutput.ReadLine();
while (str != null)
{
c_StreamOutput.BaseStream.Flush();
txtboxCNF.AppendText(str + "\r\n");
Thread.Sleep(100);
str = c_StreamOutput.ReadLine();
}
}
catch (Exception) { }
}
}

private void btnCNF_Click(object sender, System.EventArgs e)
{
if(c_Process == null)
{
c_Process = new Process();
ProcessStartInfo psi = new ProcessStartInfo("console.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.CreateNoWindow = true;
c_Process.StartInfo = psi;
c_Process.Start();

c_StreamInput = c_Process.StandardInput;
c_StreamOutput = c_Process.StandardOutput;

c_StreamInput.AutoFlush = true;

c_ThreadRead = new Thread(new ThreadStart(ReadStdOutputThreadProc));
c_ThreadRead.IsBackground = true;
c_ThreadRead.Start();
}
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (c_ThreadRead != null)
{
c_ThreadRead.Abort();
c_ThreadRead.Join();
}
if(c_Process != null && !c_Process.HasExited)
c_Process.Kill();
}

private void txtboxCNF_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyValue == 13)
{
c_StreamInput.WriteLine((char)e.KeyValue+"\0\n");
}
}

The console application that I am trying to run (console.exe) is a simple C
program that prints a line text and then calls the gets() function. The code
is basically this:

#include "stdafx.h"
#include <conio.h>
#include <string.h>

void create_it();

int main(int argc, char* argv[])
{
create_it();

return 1;
}

void create_it()
{
char str_nodes[256], str_filename[256], str_username[256], str_serial[256];

printf("Welcome to the console program\n");

/* Display maximum number of nodes */
printf ("Max Nodes : ");
gets( str_nodes);

/* get file name */
printf ("Enter user configuration file name : ");
gets (str_filename);

/* get user name */
printf ("User Name : ");
gets (str_username);

/* get serial number */
printf ("Serial Id : ");
gets (str_serial);
}

Thanks in advance for your help.
 
G

Guest

The problem Im facing is that there is not output in the textbox and the
program seems to block and hang....
 
G

Guest

===================================================
Yury said:
I think handle OutputDataReceived and ErrorDataReceived process events
is better.
===================================================


Hi Yury,

I have tried this, but I keep getting the following error message at the
call: c_Process.BeginOutputReadLine()
"Cannot mix synchronous and asynchronous operation on process stream.".

My code looks like this:

private void CNFOutputHandler(object sendingProcess, DataReceivedEventArgs
outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
txtboxCNF.AppendText(outLine.Data + "\r\n");
}
}

private void btnCNF_Click(object sender, System.EventArgs e)
{
if(c_Process == null)
{
c_Process = new Process();
ProcessStartInfo psi = new
ProcessStartInfo("C:\\XPDistribution\\console.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;

////
c_Process.OutputDataReceived += new
DataReceivedEventHandler(CNFOutputHandler);
////

psi.RedirectStandardInput = true;
psi.CreateNoWindow = true;
c_Process.StartInfo = psi;
c_Process.Start();

c_StreamInput = c_Process.StandardInput;
c_StreamOutput = c_Process.StandardOutput;

c_StreamInput.AutoFlush = true;

//c_ThreadRead = new Thread(new ThreadStart (ReadStdOutputThreadProc));
//c_ThreadRead.IsBackground = true;
//c_ThreadRead.Start();

c_Process.BeginOutputReadLine();
}
}

Any ideas what I am doing wrong ?

Thanks.
 

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

Similar Threads


Top