-1

I have two console application, the first is "Process1", which compiled as a exe and cannot change the code inside it. The second is a "meter" console app used to calc the launch time of the Process1. I want to now the time of the moment when the HelloWorld is printed in the Console windows.

// {My Process1}: //I compiled it into a assembly called process1.exe.

Class Process1{
    static void Main(String[] args]){
        Console.Write("Hello word");
    }
}

My 2nd application is an console application which call process1.exe as a Process, I want to know when the process1 is "ready", assume its the time when the "Hello Word" is printed. I read about Process.OutputDataReceived event, but the event is never fired when debugged, the code inside never printed.

// {Meter Application}

public Class Student{
    String ID;
    String Name;
}

public Class Program{
    public static void Main(String[] args){
        p.StartInfo = new ProcessStartInfo("process1.exe")
        {
            UseShellExecute = false,
            //Add this line
            RedirectStandardOutput = true,
        };
        Student st = new Student();
        p.OutputDataReceived += P_OutputDataReceived;
        p.Start();
        //Add this line
         p.BeginOutputReadLine();
        p.WaitForExit();
    }

    private static void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            string output = e.Data;
            Console.WriteLine("Process1 ready at: "+DateTime.Now);
            Console.WriteLine("Data is: " + output);

        }

}
Andiana
  • 1,912
  • 5
  • 37
  • 73
  • 1
    Maybe add a `p.WaitForExit();` after the `p.Start()`? You can also directly `Console.WriteLine(e.Data)` in the handler for debugging purposes. You also need to `RedirectStandardOutput = true` in the properties of your `ProcessStartInfo` object, otherwise the handler will never be called. – Maximilian Gerhardt Jan 17 '16 at 13:24
  • Hi @MaximilianGerhardt, I added the WaitForExit() - updated. The event still not be fired. I try to change the handle function to alsway print e.Data private static void P_OutputDataReceived(object sender, DataReceivedEventArgs e) { string output = e.Data; Console.WriteLine("Data is: " + output); } But nothing printed. – Andiana Jan 17 '16 at 13:40
  • Place breakpoints & gather – Medet Tleukabiluly Jan 17 '16 at 13:44
  • 1
    Ops, yes, since it's missing a `p.BeginOutputReadLine();` right after the `p.Start()`, see http://stackoverflow.com/a/285841/5296568 – Maximilian Gerhardt Jan 17 '16 at 13:46
  • @MaximilianGerhardt, Thankyou, its worked, I will update the code – Andiana Jan 17 '16 at 14:28

1 Answers1

1

My comments summarized:

This modification to the Main() method should do it.

public static void Main(String[] args){
    p.StartInfo = new ProcessStartInfo("process1.exe")
    {
        UseShellExecute = false,
        RedirectStandardOutput = true
    };
    Student st = new Student();
    p.OutputDataReceived += P_OutputDataReceived;
    p.Start();
    p.BeginOutputReadLine();
    p.WaitForExit();
}
Maximilian Gerhardt
  • 5,188
  • 3
  • 28
  • 61