等待命令在 C# 中完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1169910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:56:11  来源:igfitidea点击:

Waiting for the command to complete in C#

c#

提问by Naveen

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?

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

采纳答案by Jon Skeet

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_readerin 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 haveto read the output, otherwise it could fill up the output buffer and basically block the process.)

这实际上取决于命令将执行的操作。您可以等待进程退出Process.WaitForExit,同时从m_reader另一个线程或 with读取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 viathe command line, you could find that as a Processand wait for thatto exit. It's tricky though - I really would try to redesign your solution.

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

回答by Philippe Leybaert

You can call

你可以打电话

 m_command.WaitForExit();

回答by David Schmitt

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

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

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

回答by alex

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();
    }

回答by bohdan_trotsenko

You can wait for the prompt in the output.

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

Quite tricky too.

也相当棘手。