473,383 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Process & OutputDataReceived Handler

rob
Hello,

I have a DOS program that I need to execute from a WinForm application.
This DOS program will take a long time to execute. During execution it
prints out messages. While the DOS program is still running I want to
catch these messages and display them to the user. I managed to create
the process and redirect the output. The problem I am having is that
the even handler for OutputDataReceived is not called until the DOS
program is finished. Once it is finished the handler gets called for
each line. My question is why does it not get called right after each
line is printed? What am I doing wrong.

Bellow is a sample code. The DOS program prints out one line (with \n")
of random numbers every every 500ms. Proc is a function that is called
when I press a button on the WinForm.

StringBuilder Output;
static int numOutputLines=0;
private void Proc()
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
output.Output = new StringBuilder("");
p.OutputDataReceived += new
DataReceivedEventHandler(OutputHandler);

p.StartInfo.FileName = @"randomNum.exe";
p.StartInfo.CreateNoWindow = true;
p.Start();

p.BeginOutputReadLine();
while (true)
{
string s;
s = "";
int n;

//Shows empty string until process is finished
//When process is finished it shows all lines.
s = Output.ToString();
MessageBox.Show(s, "result");
}
p.Close();
}

private void OutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
//This event handler is not called until process is finished.
//When process is finished it gets called once for each line
if (!String.IsNullOrEmpty(outLine.Data))
{
numOutputLines++;
Output.Append(Environment.NewLine +
"[" + numOutputLines.ToString() + "] - " + outLine.Data);
}
}

Regards,
Rob

Nov 17 '05 #1
0 4639

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SnakeS | last post by:
Hi, I want say if exist an Event that find out when a process has start. For example with task manager, when a new process has start , it come update into list. Thanks.
3
by: Steve B. | last post by:
Hi, I'm written a Web Service that encapsulate the call to a command line application. The command line application is run using Process.Start, and I'd like to get both standard ouput and...
5
by: Mikko Nylén | last post by:
Hi!, I'm trying to read the output of an process as it gets executed and append the output to a RichTextBox. However, I have a little problem: the process I'm trying to execute is a Python...
2
by: Lyon | last post by:
Here is a part of my code: internal bool Excute(string input) { ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = this.filePathToExcute; psi.RedirectStandardOutput = true;...
0
by: Lyon | last post by:
Here is a part of my code: internal bool Excute(string input) { ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = this.filePathToExcute; psi.RedirectStandardOutput = true;...
1
by: ice9 | last post by:
I seem to be missing something in this straightforward example.. but i have a vbs file that is now simplified to one line: Wscript.Echo "test" When i run the process, i'd like output to be...
1
by: lapucca | last post by:
Hi, I'm using VS 2005, ,.net 2 for C# windows application. I'm using Process to run a C application and redirecting its standard output so I can read it with StreamReader.ReadToEnd. It's only...
0
by: =?Utf-8?B?UHVjY2E=?= | last post by:
I Have the following in my own library project and I call it from my C# application twice each time to get Unix group account information and user account information. But it would return...
3
by: =?Utf-8?B?YV93YWhvbw==?= | last post by:
I work in a group that uses a variety of third party tools to control an embedded system. I need to integrate the functionality of these tools (mostly command prompt) into a single interface. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.