473,785 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Process & OutputDataRecei ved 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 OutputDataRecei ved 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.Use ShellExecute = false;
p.StartInfo.Red irectStandardOu tput = true;
output.Output = new StringBuilder(" ");
p.OutputDataRec eived += new
DataReceivedEve ntHandler(Outpu tHandler);

p.StartInfo.Fil eName = @"randomNum.exe ";
p.StartInfo.Cre ateNoWindow = true;
p.Start();

p.BeginOutputRe adLine();
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(o bject sendingProcess,
DataReceivedEve ntArgs outLine)
{
//This event handler is not called until process is finished.
//When process is finished it gets called once for each line
if (!String.IsNull OrEmpty(outLine .Data))
{
numOutputLines+ +;
Output.Append(E nvironment.NewL ine +
"[" + numOutputLines. ToString() + "] - " + outLine.Data);
}
}

Regards,
Rob

Nov 17 '05 #1
0 4682

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

Similar topics

1
318
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
1400
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 standard error to create a log. After some searches, I finally wrote this part of code : public class MyClass {
5
11407
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 script, which uses the time.sleep() to sleep for a specified amount of time. This (the use of time.sleep()) seems to have the side effect of the output not being added to the RichTextBox until the process has finished, which it won't do as the...
2
5738
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; psi.RedirectStandardError = true;
0
1477
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; psi.RedirectStandardError = true;
1
10398
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 returned asynchronously since i will be running this script many times with differetn parameters and collecting return strings. my return "test" text actually work in synchronous mode: process.StandardOutput.ReadToEnd()
1
3090
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 reading the 1st 5 lines or so when there is about 100 lines of data. I also tried using the Process.BeginOutputReadlin and a DataReceivedEvntHandler to gather output but I also only get about 5 lines of the data. There seems to be a buffer...
0
1295
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 different number of records (lines of data read), sometimes 0 records without any of my message, and there doesn't seem to be a pattern to be found. Can someone have a look and see if any of my code could possbly cause this? Thanks. static...
3
1731
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 followed the examples on MSDN and other sites to create a class that spawns a System.Diagnostics.Process with re-directed StdIn, StdOut, and StdErr. I read the user input from a textbox and write the output to another textbox. This approach...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.