[Mono-dev] fail to read async process output

YyYo yossiozani at gmail.com
Wed Nov 26 09:33:19 EST 2008


Hi All

I solve the problem by adding to the C program(echoer.c) the command
fflush(stdout);
BTW: It seems that under windows there is no need to call fflush(), probably
because the system call fprintf do also fflush() by default, and under Linux
I must add the command fflush()
echoer.c code:
#include<stdio.h>
#include<stdlib.h>
int main(){
	int i;
	for(i=0;i<10;i++){
		printf("hello world\n");
                fflush(stdout);
		sleep(1);
	}
	return 0;
}



YyYo wrote:
> 
> Hi All...
> 
> I just install mono and wanted to write a simple program which execute
> another program and get his output asynchronously.
> My program use Process class and I append an appropriate method, to get
> the output, to OutputDataReceived event.
> 
> When I run the program under windows, with .NET and it work perfectly.
> When I use mono under linux(ubuntu) the output of the program doesn't show
> async. All the output print to screen at once, only when the program is
> finished, instead of printing it when needed.
> 
> Here is the code:
> using System;
> 
> using System.Collections.Generic;
> using System.Text;
> using System.Diagnostics;
> using System.Threading;
> using System.IO;
> namespace processOutput
> 
> {
>     class Program
>    {
>         public static void Worker_OutputDataReceived(Object sender,
> DataReceivedEventArgs outLine)
>         {
>             if (!String.IsNullOrEmpty(outLine.Data))
>                 Console.WriteLine("Process Output: {0}",outLine.Data);
>         }
> 
>         static void Main(string[] args)
>         {
>             Process Worker = new Process();
>             Worker.StartInfo.RedirectStandardOutput = true;
>             Worker.StartInfo.UseShellExecute = false;
>             Worker.StartInfo.FileName = @"/home/yossioz/C/echoer";
>             Worker.OutputDataReceived+=new
> DataReceivedEventHandler(Worker_OutputDataReceived);
>             Worker.Start();
>             Worker.BeginOutputReadLine();   // Must called after the
> process started
>             Worker.WaitForExit(); 
>        }
> 
>     }
> 
> }
> 
> The echoer program is a simple C program which print in loop "hello world"
> and wait for 1 second in each time.
> echoer code:
> 
> #include<stdio.h>
> #include<stdlib.h>
> int main(){
> 	int i;
> 	for(i=0;i<10;i++){
> 		printf("hello world\n");
> 		sleep(1);
> 	}
> 	return 0;
> }
> 
> Please help me to figure it out.
> 
> Yossi
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/fail-to-read-async-process-output-tp20636533p20701860.html
Sent from the Mono - Dev mailing list archive at Nabble.com.



More information about the Mono-devel-list mailing list