[RESOLVED] How to run a process and get its output continously?
consoletest.exe
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int i;
for (i = 0; i < 10000; i++)
Console.Out.WriteLine(i.ToString());
}
}
}
windows project
Code:
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.DoEvents();
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo pi = new System.Diagnostics.ProcessStartInfo();
pi.FileName = "C:\\consoletest.exe";
pi.Arguments = "C:\\mailbox";
pi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pi.RedirectStandardOutput = true;
pi.UseShellExecute = false;
proc.StartInfo = pi;
proc.Start();
System.IO.StreamReader myOutput = proc.StandardOutput;
proc.WaitForExit();
if (proc.HasExited)
{
string output = myOutput.ReadToEnd();
textBox1.Text = output;
}
}
The question is
How do I run the console program in background and update its output in real time to the textbox?
The windows program should also continue accepting other Events while the console process is being executed.
Re: How to run a process and get its output continously?
The way I see your code working atm is, the console only updates the textbox whenever the button is clicked.
What you could do I suppose is create a service that will continuouly watch for changes on mailbox and as soon as there is a change on mailbox, it will run the console app and then update the textbox within the form for the service.
The processes within the service will need to run in threads though and if you want the application to queue the events and process them as them come along, then use a threadpool to process it.
Regards,
Re: How to run a process and get its output continously?
Sorry, it won't solve my problem..
Anyone knows the solution?
Re: How to run a process and get its output continously?
Re: How to run a process and get its output continously?
Quote:
Originally Posted by
axion_sa
Thanks..
Problem solved :)
What I needed was just an event handler
The following snippet is for the benefit of the people who might find this thread in the future
Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void threadmethod()
{
Process process;
process = new Process();
process.StartInfo.FileName = "C:\\consoletest.exe"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.Close();
}
private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
SetTextBoxText(outLine.Data.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
Thread thread1 = new Thread(threadmethod);
thread1.Start();
}
private delegate void SetTextBoxTextInvoker(string text);
private void SetTextBoxText(string text)
{
if (this.textBox1.InvokeRequired)
{
this.textBox1.Invoke(new SetTextBoxTextInvoker(SetTextBoxText), text);
}
else
{
this.textBox1.Text = text;
}
}
}