Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for the command to complete in C#

Tags:

c#

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?

like image 374
Naveen Avatar asked Jul 23 '09 06:07

Naveen


People also ask

What does wait () do in C?

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.

What does the wait () system call return on success?

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?

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".

How do you use wait?

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.


2 Answers

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.

like image 75
Jon Skeet Avatar answered Oct 13 '22 06:10

Jon Skeet


You can call

 m_command.WaitForExit();
like image 22
Philippe Leybaert Avatar answered Oct 13 '22 06:10

Philippe Leybaert