none
While running cmd through System.Diagnostics.Process is not giving me ouput instantly

    Question

  • Hello,

    I am using following code 

                            var proc1 = new System.Diagnostics.Process();

                            proc1.EnableRaisingEvents = false;
                            proc1.StartInfo.CreateNoWindow = true;
                            proc1.StartInfo.FileName = "External.exe";
                            proc1.StartInfo.Arguments = "\"MyArguments\"";
                            proc1.StartInfo.WorkingDirectory = RunningProject.Path;
                            proc1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                            proc1.StartInfo.RedirectStandardOutput = true;
                            proc1.StartInfo.RedirectStandardError = true;
                            proc1.StartInfo.UseShellExecute = false;
                            proc1.EnableRaisingEvents = true;
                            proc1.Exited += new EventHandler(proc1_Exited);
                            proc1.ErrorDataReceived += new DataReceivedEventHandler(proc1_ErrorDataReceived);
                            proc1.OutputDataReceived += new DataReceivedEventHandler(proc1_OutputDataReceived);

                            proc1.Start();

                            proc1.BeginErrorReadLine();
                            proc1.BeginOutputReadLine();

                            proc1.WaitForExit();
                            proc1.Close();

    When i execute this code i am not getting output data from External.exe instantly. I have to wait for about 2 to 3 mins and then output is received to below event.

    void proc1_OutputDataReceived(object sender, DataReceivedEventArgs e)
            {
                if (e.Data != null && RunningProject != null)
                {
                    
                }
            }

    But when i run External.exe in command prompt directly then i could see output is written to command prompt instantly without any delay.

    I read some where that if i use System.Diagnostics.Process to run same as command prompt then it uses buffer concept and when buffer is filled then only it pushes data to StandardOutput stream. If this is true then, is their any solution to avoid sending output data to buffer instead sending directly to StandardOutput so that i get oupput data instantly with out any delay?

    Please help me on this.

    Thursday, April 20, 2017 8:13 AM

All replies

  • Try doing the operations inside a thread:

       var t = new Thread( new ThreadStart( ( ) =>
          {
             var proc1 = new System.Diagnostics.Process( );
             . . .
             proc1.Close( );
          } ) );
       t.Start();

    Do not call t.Join() here.

    Thursday, April 20, 2017 11:26 AM
  • Problem for me not about thread but getting data from process line by line.

    I am already running process in thread so.

    But i another info is i am running C++ exe to get output in console. So is their any issue with writing data to console from C++ should be the issue?

    Thursday, April 20, 2017 11:55 AM