I am new to C# and trying to develop a small application which internally opens a command prompt and executes some command here. This is what I have done so far:
m_command = new Process();
m_command.StartInfo.FileName = @"cmd.exe";
m_command.StartInfo.UseShellExecute = false;
m_command.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
m_command.StartInfo.CreateNoWindow = true;
m_command.StartInfo.RedirectStandardInput = true;
m_command.StartInfo.RedirectStandardOutput = true;
m_command.Start();
m_reader = m_command.StandardOutput;
m_writer = m_command.StandardInput;
m_writer.WriteLine("Somecommand"); //execute some command
As you can see, I have redirected the input and output. My question is how do I execute the "some command" synchronously i.e. I want to read the result of my command using the redirected output. For that I have to wait until the command I invoked using WriteLine to complete. How do I do that?
A call to wait() blocks the calling process until one of its child processes exits or a signal is received. After child process terminates, parent continues its execution after wait system call instruction.
wait(): on success, returns the process ID of the terminated child; on error, -1 is returned. waitpid(): on success, returns the process ID of the child whose state has changed; on error, -1 is returned; if WNOHANG was specified and no child(ren) specified by pid has yet changed state, then 0 is returned.
How do you make a C program pause for a few seconds? Insert, wherever you need your program to make a delay: sleep(1000); Change the "1000" to the number of milliseconds you want to wait (for example, if you want to make a 2 second delay, replace it with "2000".
Wait means 'stay in the same place or not do something until something else happens'. We can use it with or without for: Put a tea bag into the cup, then add water and wait (for) a minute or two before taking it out. I phoned the head office but I had to wait (for) five minutes before I spoke to anyone.
That really depends on what the command will do. You could wait for the process to exit with Process.WaitForExit
, while simultaneously reading from m_reader
in another thread or with OutputDataReceived
. That will only work if the process is going to quit when the command has finished though. (Note that you have to read the output, otherwise it could fill up the output buffer and basically block the process.)
The other option is if you'll get a certain bit of output when the command has finished - such as the next command prompt. The trouble is that if your command happens to output the same thing, you'll get it wrong.
It feels like launching a command prompt like this isn't a great approach though. Any reason you don't create a separate process each time?
Another option: if you can work out what process you've just launched via the command line, you could find that as a Process
and wait for that to exit. It's tricky though - I really would try to redesign your solution.
You can call
m_command.WaitForExit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With