I use gdb to get some debug information in the program.

alt text

I enter into the terminal "gdb gs", then there is some processing. When processing is completed climbs out (gdb), they say we are waiting, you can enter the command further.

There is a code:

ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "gdb"; psi.Arguments = "gs"; psi.RedirectStandardOutput = true; psi.RedirectStandardInput = true; psi.UseShellExecute = false; Process p = Process.Start(psi); while (!p.StandardOutput.EndOfStream) { string _out = p.StandardOutput.ReadLine(); Console.WriteLine(_out); if (обработка завершена) p.StandardInput.WriteLine("info variables"); } 

In the while loop, each new line is read as it arrives, the last line "... done." I don’t get any new lines anymore, there’s no "(gdb)" line, so the waiting continues in the loop. The problem is that I do not know how to understand when the processing was completed. You can check the end of the line, if "done.", Then continue, but it does not fit. It is necessary to receive event of appearance "(gdb)".

  • one
    Thanks for the answer. GDB does not complete, it hangs and waits for internal commands, the process remains. And the problem was solved by the fact that instead of ReadLine () I used Read () (character-by-character), but (gdb) did not read, because the line had not yet been completed, but the characters were visible. - astrel

1 answer 1

If I understand you correctly, then you need to catch the completion event "gdb". In this case, enable EnableRaisingEvent and hang EventHandler on Exited.

 using System; using System.Diagnostics; using System.Threading; class ProcessClass { public ProcessClass() { this.ProcessToExec = new Process(); } private Process ProcessToExec { get; set; } private bool EventHandled { get; set; } // Print a file with any known extension. public void Execute(string fileName, string args) { this.EventHandled = false; try { // Start a process to print a file and raise an event when done. this.ProcessToExec.StartInfo.FileName = fileName; this.ProcessToExec.StartInfo.Arguments = args; this.ProcessToExec.StartInfo.RedirectStandardOutput = true; this.ProcessToExec.StartInfo.RedirectStandardInput = true; this.ProcessToExec.StartInfo.UseShellExecute=true; this.ProcessToExec.EnableRaisingEvents = true; this.ProcessToExec.Exited += new EventHandler(processToExec_Exited); this.ProcessToExec.Start(); } catch (Exception ex) { Console.WriteLine("An error occurred trying to execute \"{0}\":" + Environment.NewLine + ex.Message, fileName); return; } // Wait for Exited event while (!this.EventHandled) { string _out = this.ProcessToExec.StandardOutput.ReadLine(); Console.WriteLine(_out); } this.ProcessToExec.StandardInput.WriteLine("info variables"); } // Handle Exited event private void processToExec_Exited(object sender, System.EventArgs e) { this.EventHandled = true; } public static void Main(string[] args) { if (args.Length <= 0) { Console.WriteLine("Enter a file name."); return; } // Create the process ProcessClass myProcess = new ProcessClass(); myProcess.Execute(args[0], (args.Length==2 ? args[1]:string.Empty)); } }