Run the commands and wait for completion in C #

I need to do this in my C # program.

           Process process = LaunchCommandWindow("Special args and environment set")
           process.StandardInput.WriteLine("cmd1");
           process.StandardInput.WriteLine("cmd2");

           //Parse logs
            using (Streamreader sr =  new ())
           { etc etc..}

My commands execute correctly, but the problem is that the Parse log code does not wait for cmd1 and cmd2 to complete. I can’t name the way out and the process. waitforexit because after parsing the logs I have more commands to execute.

Here's how to set up process startup information. LaunchCommandWindow ()

         ProcessStartInfo startInfo = new ProcessStartInfo();

        startInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
        startInfo.Arguments = "/K " + newEnvironmentArgs;

        startInfo.UseShellExecute = false; 
        startInfo.RedirectStandardInput = true;
        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();

        return process;

How do I get the parsing log code to wait for cmd2 to complete?

Thank.

+3
source share
3 answers

If "cmd2" writes the completion to the standard output stream, you can listen to the standard output until you see this event, and then process it.

+3
source

.

BackgroundWorker.

, ( ), .

, , , .

----- -----

, : RedirectStandardOutput true, .

, process.WaitForExit(int) int - , , ( process ) cmd2.

+1

, Process.WaitForExit() - , . Process.HasExited.
- "cmd/C cmd1 cmd2"

0

Source: https://habr.com/ru/post/1789322/


All Articles