|
-
Jul 8th, 2009, 08:46 PM
#1
Thread Starter
Addicted Member
[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.
Last edited by winterslam; Jul 12th, 2009 at 06:50 AM.
-
Jul 9th, 2009, 06:06 AM
#2
Hyperactive Member
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,
Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered 
If someone helped you today then please consider rating their post.
-
Jul 10th, 2009, 08:30 AM
#3
Thread Starter
Addicted Member
Re: How to run a process and get its output continously?
Sorry, it won't solve my problem..
Anyone knows the solution?
-
Jul 10th, 2009, 11:37 AM
#4
Re: How to run a process and get its output continously?
-
Jul 12th, 2009, 06:48 AM
#5
Thread Starter
Addicted Member
Re: How to run a process and get its output continously?
 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;
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|