threading part 2

J

Jeff Louie

Second try. By the way does anyone know where cerr is going in C++. In
debug mode, Visual Studio 2003 cerr is not going to the console.

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace TestThreadedConsole
{

/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
StreamReader my_cout;
StreamReader my_cerr;
StreamWriter my_cin;
Process myProcess = new Process();
Thread thread_cout;
Thread thread_cin;
Thread thread_cerr;

public void Exit()
{
if (thread_cout.IsAlive){thread_cout.Abort();}
if (thread_cin.IsAlive) {thread_cin.Abort();}
if (thread_cerr.IsAlive) {thread_cerr.Abort();}
myProcess.Close();
}
public void method_cout()
{
while(!myProcess.HasExited)
{
Console.WriteLine(my_cout.ReadLine());
}
}
public void method_cin()
{
while(!myProcess.HasExited)
{
my_cin.WriteLine(Console.ReadLine());
}
}
public void method_cerr()
{
while(!myProcess.HasExited)
{
Console.WriteLine(my_cerr.ReadLine());
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//// TODO: Add code to start application here//
///
new Class1();
}
public Class1()
{
ProcessStartInfo myProcessStartInfo = new
ProcessStartInfo("ConsoleServer.exe" );
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.RedirectStandardError= true;
myProcessStartInfo.RedirectStandardInput= true;
//myProcessStartInfo.Arguments= "";

myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();

my_cout= myProcess.StandardOutput;
my_cerr= myProcess.StandardError;
my_cin= myProcess.StandardInput;

thread_cout= new Thread(new ThreadStart(this.method_cout));
thread_cout.IsBackground= true;
thread_cin= new Thread(new ThreadStart(this.method_cin));
thread_cin.IsBackground= true;
thread_cerr= new Thread(new ThreadStart(this.method_cerr));
thread_cerr.IsBackground= true;

thread_cout.Start();
thread_cin.Start();
thread_cerr.Start();

myProcess.WaitForExit();
Exit();
}
}
}
 
D

David Levine

cerr gets written to when the console app writes to StdError, not StdOut.
Some apps use this a lot, others never use it. However, you want to read
from it anyway just in case one does write to it because if the buffer fills
up then the app will block until it is drained, and if it never drains it
effectively locks up the app.

You should probably start reading from the outputs before starting the
process.
 
J

Jeff Louie

David... I just tried that and it throws an exception. The only way I
can get this to work is to call myProcess.Start first.

Regards,
Jeff
You should probably start reading from the outputs before starting the
process.<
 
D

David Levine

My bad. I went back and checked my code...I also start the process before
actually trying to read from StdOut or StdErr.
 

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