Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

External program run delay 1

Status
Not open for further replies.

kokon

Programmer
Apr 27, 2006
31
PL
Hi, when I start from application my external program (execution takes 4 min) it wait until my external program exit, then i see my application window with textbox with the external program output. How to make textbox update in real time. source:

---------
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("text.exe");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcessStartInfo.CreateNoWindow = true;

myProcess.Start();

StreamReader myStreamReader = myProcess.StandardOutput;
// Read the standard output of the spawned process.
string myString = myStreamReader.ReadToEnd();
Console.WriteLine(myString);
richTextBox1.Text = myString;
myProcess.Close();

-----------------------
thx for help
 
myStreamReader.ReadToEnd(); will cause your app to wait until the program exits.

if you loop through the output as it is streaming you can update your textbox in "real time"

string myString = myStreamReader.Read();

while (myString != null)
{
richTextBox1.Text += myString + System.Environment.NewLine();

myString = myStreamReader.Read();
}
 
sorry but it is not working, i have tried to write whole code inside new thread but it doesnt help. Any idea?
 
I could not sleep and decided to solve this for you. I have created a project in VS2005 which lies here. Remember to change the exe that should be started in the cAppStart class. You can download the solution here:


Basicly I have this class which starts the process and raise an event when output is read:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace WindowsApplication3
{
class cAppThread
{
public delegate void DelegateOutputReceived(string output);
public static event DelegateOutputReceived OnOutputReceived;
public void Run()
{
string output = null;
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("c:/outputtest.exe");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcessStartInfo.CreateNoWindow = true;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadLine();

while (myString != null)
{
output += myString + System.Environment.NewLine;
myString = myStreamReader.ReadLine();
OnOutputReceived(output);
}
}
}
}

The class is started in a new thread here:

private void StartApp()
{
cAppThread.OnOutputReceived += new cAppThread.DelegateOutputReceived(OutPutReceived);
cAppThread at = new cAppThread();
Thread t = new Thread(new ThreadStart(at.Run));
t.IsBackground = true;
t.Start();
}

The function that handles the event is here:

void OutPutReceived(string output)
{
if (InvokeRequired)
{
object[] obj = new object[1];
obj[0] = output;
cAppThread.DelegateOutputReceived del = new cAppThread.DelegateOutputReceived(OutPutReceived);
this.BeginInvoke(del, obj);
}
else
{
richTextBox1.Text = output;
}

}
 
By the way, it was tested against the following console app code:

static void Main(string[] args)
{
for (int i = 0; i < 30; i++)
{
Console.WriteLine(i);
System.Threading.Thread.Sleep(1000);
}
}

It requires that there is a new line after each output - otherwise the code has to be slight changed.
 
Thx for your help:) and sorry about sleepless night :) By the way, your code is going to slow down when the external program generates really big output... any ideas? Once again thx for help, and i`m looking forward for your reply...
 
Well, it is probably GUI that takes most CPU/slows down. So I recommend that you use a grid instead or listview. Also, you should limit that listview or grid to a number of lines so that it wont get too big.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top