2

In C#, I am trying to run a command line program that gets input from stdin and then it returns the output to stdout . I need to keep cmd.exe running (using /k) and in a for loop, send in text and then wait for the output before sending the next text. This works if I don't redirect stdout but not after I redirect it. Initially I got data back from stdout (although much later) and now that is no longer working other than the initial call to start the program. This link says "Alternately, you can avoid the deadlock condition by creating two threads and reading the output of each stream on a separate thread." Would that fix my issue and if so, how would I do that?

The code is set up as follows:

StringBuilde  sb = new StringBuilder();
    Process process = new Process();
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.WorkingDirectory = workingdirectory;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.Arguments = "/k";
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardInput = true;    
        process.EnableRaisingEvents = true;
        process.OutputDataReceived += (sender, args) => AppendData(args.Data); //this appends the output to a stringbuilder
        process.ErrorDataReceived += (sender, args) => AppendError(args.Data);

    process.Start();  

        //sw is a streamwriter
        sw= process.StandardInput;

    //now call the command line code 
        sw.WriteLine(" some.exe some.arg ");

    process.BeginOutputReadLine();

        foreach(DataRow row in dtMydata.Rows)
    {   
        mytext=row["Text"].toString();

        sw.WriteLine(mytext);  
        sw.WriteLine(Environment.NewLine); //This redirects the text to the program,

    }
dmkoher
  • 21
  • 2
  • As I recall, if you want to redirect both STDIN and STDOUT you need to do so asynchronously. I think there is a .NET example at [ProcessStartInfo.RedirectStandardError](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandarderror?redirectedfrom=MSDN&view=netframework-4.7.2#System_Diagnostics_ProcessStartInfo_RedirectStandardError). – Dour High Arch Mar 14 '19 at 19:28
  • Hi, thanks for your input. I added the process.BeginErrorReadLine() for an asychronous read on the error stream as well and I am still not getting the stdout stream once the program starts running. I have a sync call to get stdin, and asynch on stdout and stderr. I think I need to explore how to run these in separate threads. That is suggested on the link you provided as well as the link I refernenced above. – dmkoher Mar 15 '19 at 12:16
  • In looking closer, in the debug "Cannot mix synchronous and asynchronous operation on process stream." as soon as I execute the asynch call process.BeginOutputReadLine(). Need to investigate this further. – dmkoher Mar 15 '19 at 13:25
  • I'm about to give up on this... using my code as well as many of the example programs, when I call the async method process.BeginOutputReadLine() right after start, I get an error "Cannot mix synchronous and asynchronous operation on process stream." – dmkoher Mar 15 '19 at 18:56

0 Answers0