等待命令在C#中完成

时间:2021-08-14 00:32:03

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:

我是C#的新手,并尝试开发一个小型应用程序,在内部打开命令提示符并在此处执行一些命令。这是我到目前为止所做的:

    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?

如您所见,我已重定向输入和输出。我的问题是如何同步执行“some命令”,即我想使用重定向输出读取命令的结果。为此,我必须等到我使用WriteLine调用的命令才能完成。我怎么做?

5 个解决方案

#1


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

这实际上取决于命令的作用。您可以等待进程退出Process.WaitForExit,同时从另一个线程中的m_reader或使用OutputDataReceived读取。只有在命令完成后进程退出时,这才有效。 (注意,您必须读取输出,否则它可能会填满输出缓冲区并基本上阻止该过程。)

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.

另一个选择:如果您可以通过命令行计算出刚刚启动的进程,您可以找到它作为进程并等待退出。虽然这很棘手 - 我真的会尝试重新设计你的解决方案。

#2


You can call

你可以打电话

 m_command.WaitForExit();

#3


This is what I used in a small project I made:

这是我在一个小项目中使用的:

processStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = true;
    startInfo.UseShellExecute = false;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = command;

// Start the process with the info we specified.
    // Call WaitForExit and then the using statement will close.
    using(Process exeProcess = Process.Start(startInfo))
    {
        exeProcess.WaitForExit();
    }

#4


The easiest way would be to put all commands into a .cmd file, execute that and use

最简单的方法是将所有命令放入.cmd文件,执行并使用

 Process.Start("cmdfile").WaitForExit();

#5


You can wait for the prompt in the output.

您可以在输出中等待提示。

Quite tricky too.

非常棘手。

#1


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

这实际上取决于命令的作用。您可以等待进程退出Process.WaitForExit,同时从另一个线程中的m_reader或使用OutputDataReceived读取。只有在命令完成后进程退出时,这才有效。 (注意,您必须读取输出,否则它可能会填满输出缓冲区并基本上阻止该过程。)

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.

另一个选择:如果您可以通过命令行计算出刚刚启动的进程,您可以找到它作为进程并等待退出。虽然这很棘手 - 我真的会尝试重新设计你的解决方案。

#2


You can call

你可以打电话

 m_command.WaitForExit();

#3


This is what I used in a small project I made:

这是我在一个小项目中使用的:

processStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = true;
    startInfo.UseShellExecute = false;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = command;

// Start the process with the info we specified.
    // Call WaitForExit and then the using statement will close.
    using(Process exeProcess = Process.Start(startInfo))
    {
        exeProcess.WaitForExit();
    }

#4


The easiest way would be to put all commands into a .cmd file, execute that and use

最简单的方法是将所有命令放入.cmd文件,执行并使用

 Process.Start("cmdfile").WaitForExit();

#5


You can wait for the prompt in the output.

您可以在输出中等待提示。

Quite tricky too.

非常棘手。