I have C# app1 that starts the C# console app2 and reads the console standard output values of it. Normally I am reading the outputs with code below (app1)

    public void read_values_from_app2
    {    
       Process_OPCDA = new Process                
       {
           StartInfo = new ProcessStartInfo
           {
               FileName = MTX_OPC_DA_reader_path,
               Arguments = arguments,
               UseShellExecute = false,
               RedirectStandardOutput = true,
               CreateNoWindow = true
           }
       };
       Process_OPCDA.Start();
       string item_values = Process_OPCDA.StandardOutput.ReadToEnd();
       Process_OPCDA.WaitForExit();                     
     }

This is working but I am reading the outputs with 4-5 seconds delay because the console app (app-2) connects first to remote CNC machine, reads the required values, disconnects and then exits. Here the disconnect step takes up to 4-5 seconds. So although the values are there I have to wait additionally 4-5 seconds until I get the values in my app-1, because of the Process_OPCDA.WaitForExit() command in my app-1.

Is there a way to get the console output, as soon as the output is written without waiting the console app to exit?

My console app looks like

    static void Main(string[] args)
    {
      // Connecting to a remote maachine
      // Reading values from it
      Console.WriteLine(values);
      // Disconnecting from the machine (takes 4-5 seconds)
      Environment.Exit(0);
    }

When I cancell the line Process_OPCDA.WaitForExit() in my app-1 code I cannot read anything. So how should revise my code in app-1?

1

There are 1 best solutions below

1
Linkruste Addict On

You could do it by calling a DLL in your writing program and inject your reading program into it / creating the reading program as a DLL