[Mono-devel-list] System.Diagnostics.Process redirection

Colin JN Breame colin at breame.net
Sun Jul 3 09:10:15 EDT 2005


Hello,

Below is a program that on my system replicates an unexpected exit by the mono 
runtime.

The lines of interest are:

	stdout.WriteLine("1");
	p.StandardInput.WriteLine("hello world!");
	stdout.WriteLine("2");

As the program runs, it prints '1' but exit before '2' is printed.

Any ideas what might be going on here?  Could this be SIGPIPE related?

 -- Colin

ps. mono 1.1.8.1



using System;
using System.IO;
using System.Diagnostics;

public class main_t {

        public static void Main() {
                while (true) {
                        run();
                }
        }

        public static void run() {
                Process p = null;

                try {
                        p = new Process();

                        string pipe = "echo hello";

                        int i = pipe.IndexOf(' ');
                        if (i == -1) {
                                p.StartInfo.FileName = pipe;
                        } else {
                                p.StartInfo.FileName = pipe.Substring(0, i);
                                p.StartInfo.Arguments = pipe.Substring(i+1, 
pipe.Length-i-1);
                        }

                        p.StartInfo.UseShellExecute = false;
                        p.StartInfo.RedirectStandardInput = true;
                        try {
                                p.Start();
                        } catch (Exception e) {
                                Console.Error.WriteLine("could not execute: 
{0}", pipe);
                                goto init_error;
                        }

                        TextWriter stdout = Console.Out;
                        Console.SetOut(p.StandardInput);
                        stdout.WriteLine("1");
                        p.StandardInput.WriteLine("hello world!");
                        stdout.WriteLine("2");
                        Console.SetOut(stdout);

                        p.StandardInput.Close();

                        if (p != null) {
                                p.WaitForExit();
                        }

                } finally {
                        if (p != null) p.Close();
                }
        init_error:;
        }
}



More information about the Mono-devel-list mailing list