J
Jeff Louie
Well I wonder if my old brain can handle threading. Dose this code look
reasonable.
Regards,
Jeff
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;
Thread thread_loop;
public void TryToExit()
{
while (!myProcess.HasExited)
{
Thread.Sleep(100);
}
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_loop= new Thread(new ThreadStart(this.TryToExit));
thread_cout.Start();
thread_cin.Start();
thread_cerr.Start();
thread_loop.Start();
}
}
}
reasonable.
Regards,
Jeff
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;
Thread thread_loop;
public void TryToExit()
{
while (!myProcess.HasExited)
{
Thread.Sleep(100);
}
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_loop= new Thread(new ThreadStart(this.TryToExit));
thread_cout.Start();
thread_cin.Start();
thread_cerr.Start();
thread_loop.Start();
}
}
}