That is indeed VERY interesting! Thanks for the tip.
jancg
Posts
-
Any Microsoft (or other) certified coding courses worth taking? -
HOWTO: start, get partial output and kill a console application from c#Thanks for your tips. Still no luck. Code based on your clear tips below. I must admit that I didn't expect this to work since it is essentially the same as my previous attempt. In my previou spost I mentioned that while running the console program from a command window displayed output neatly; every now and then a line of text. When adding
" > output.log"
to the command line, all output winds up in output.log but NOT UNTILL the console program finishes! So here we have the same behaviour as in my c# program! Perhaps the cause is the fact that this is not a native windows console program; it need cygwin1.dll to work. Might this cause the strange behaviour?using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private Process process;
private void Form1_Load(object sender, EventArgs e)
{
process = new Process();
process.StartInfo.FileName = @"D:\Users\Jan C. de Graaf\Documents\Othello\test\zebra.exe";
process.StartInfo.Arguments = "-b 0 -h 24 -l 28 30 30 28 30 30 -r 0 -slack 0.0 -learn 26 22 -private -log hqbook.txt -test";
process.StartInfo.WorkingDirectory = @"D:\Users\Jan C. de Graaf\Documents\Othello\test";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();var thread = new Thread(new ThreadStart(GetOutputAndDisplay));
thread.Start();textBox1.Text = "Started!\r\n";
}void GetOutputAndDisplay()
{
string output;
do
{
output = process.StandardOutput.ReadLine();
textBox1.Invoke((MethodInvoker)delegate { textBox1.Text += output + "\r\n"; });
}
while (output != null);
}
}
} -
HOWTO: start, get partial output and kill a console application from c#The code below will display "Started!" in the textbox immediately. Next it will hit the TimerCallBack method every second after the initial 5 seconds of computation. As it is supposed to. Saidly Peek() return -1 up to the moment the console program finishes:( I know for sure output has already been written! If I test the same from a command window, adding " > output.log" to the command line, the output goes into output.log but not untill the program finishes :( If I start the program normally from a command window, output is written to the console line-by-line!
using System;
using System.Diagnostics;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{public Form1()
{
InitializeComponent();
}private Process process;
private void Form1_Load(object sender, EventArgs e)
{
process = new Process();
process.StartInfo.FileName = @"D:\Users\Jan C. de Graaf\Documents\Othello\test\zebra.exe";
process.StartInfo.Arguments = "-b 0 -h 24 -l 28 30 30 28 30 30 -r 0 -slack 0.0 -learn 26 22 -private -log hqbook.txt -test";
process.StartInfo.WorkingDirectory = @"D:\Users\Jan C. de Graaf\Documents\Othello\test";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();new System.Threading.Timer(new System.Threading.TimerCallback(TimerCallback), null, 5000, 1000);
textBox1.Text = "Started!\r\n";
}void TimerCallback(object state)
{
while (process.StandardOutput.Peek() > -1)
{
var data = process.StandardOutput.ReadLine();
textBox1.Invoke((MethodInvoker)delegate{textBox1.Text += data + "\r\n";});
}
}
}
} -
HOWTO: start, get partial output and kill a console application from c#I will post some code this evening when I continue experimenting at home. I already tried using the available events on the Process class but those didn't fire untill the console application was finished. I will try a background thread this evening and see how far I get. Thanks.
-
HOWTO: start, get partial output and kill a console application from c#Thank for your quick response! I can run the console program from a command window yes. >> with a more option pipe What is a "more option pipe" ? Is this like 'tail' in most unixes? Can you give an example please. I was hoping to use C# and the Process class and the RedirectStandardOutput option. But ran into the problem I described in my original question. Jan.
-
HOWTO: start, get partial output and kill a console application from c#All, I have a console application that's not mine; it cannot be modified. After starting it with a bunch of command line parameters, it computes something and displays the result on it's console. I would like to get the PARTIAL output and then kill it. I have found several code examples that seemed promising. My problem is that I can't seem to read any output before the program is finished and I need partial output to decide to kill it, since it will compute much more that I don't need after the initial computations. Jan C. de Graaf