[Mono-dev] fail to read async process output

YyYo yossiozani at gmail.com
Sun Nov 23 08:57:34 EST 2008


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-tp20636533p20636533.html
Sent from the Mono - Dev mailing list archive at Nabble.com.



More information about the Mono-devel-list mailing list