15

我正在使用 VBOXMANAGE 来“导出”来宾机器。VBOXManage 是一个控制台应用程序,可以从主机控制来宾机器的行为。由于导出命令是一个很长的过程,它返回的过程更新如下:

0%...10%...20%...30%...100%

我正在编写一个 C# 应用程序,它将使用 Process 调用 VBOXManage。这是我的代码:

Process VBOXProc = new Process();

VBOXProc.StartInfo.FileName = VBOXMANAGE;
VBOXProc.StartInfo.Arguments = Arguments;
VBOXProc.StartInfo.UseShellExecute = false;
VBOXProc.StartInfo.CreateNoWindow = true;
VBOXProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
VBOXProc.StartInfo.RedirectStandardError = true;
VBOXProc.StartInfo.RedirectStandardOutput = true;

VBOXProc.OutputDataReceived += new DataReceivedEventHandler(VBOXProc_OutputDataReceived);
VBOXProc.ErrorDataReceived += new DataReceivedEventHandler(VBOXProc_ErrorDataReceived);

VBOXProc.EnableRaisingEvents = true;

VBOXProc.Start();
VBOXProc.BeginOutputReadLine();
VBOXProc.BeginErrorReadLine();

VBOXProc.WaitForExit();

这很好,除了输出是按 LINE 读取的。这意味着流程更新“0%...10%...20%...30%...100%”只会在实际流程完成后显示。

有没有办法实时捕获控制台输出?

谢谢!

4

4 回答 4

8

这对我有用:

process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;

process.ErrorDataReceived += (sendingProcess, errorLine) => error.AppendLine(errorLine.Data);
process.OutputDataReceived += (sendingProcess, dataLine) => SetMessage(dataLine.Data);

process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();

process.WaitForExit();

error.AppendLine()并且SetMessage()是我使用的方法。

于 2013-07-23T16:26:47.217 回答
7

您可以使用所有标准 Stream 方法直接从流程的标准输出/错误中读取,只需确保将 StartInfo.Redirectxxx 设置为 true。

var p = new Process()
p.StartInfo.UseShellExecute = false;  //not sure if this is absolutely required
p.StartInfo.RedirectStandardOuput = true;
....

do
{
  Thread.Sleep(nnn);
  Console.Out.Write(p.StandardOutput.ReadToEnd());
}
while (!p.HasExited);
//catch any leftovers in redirected stdout
Console.Out.Write(p.StandardOutput.ReadToEnd());

以上将子进程的输出回显到您的应用程序标准输出。

您可以使用 p.StandardOutput.Read(char[], int, int) 读取特定大小的块,也可以使用 p.StadardOutput.BaseStream.BeginRead(...) 进行异步读取。

所有相同的方法都可用于 StandardError。

在循环中休眠可以为其他任务释放处理器,并允许一些数据在缓冲区中累积。如果睡眠时间过长,缓冲区溢出,执行进程的一些输出将会丢失。如果睡眠时间太短,则会花费大量 CPU 周期来读取和清空缓冲区。

于 2010-02-25T07:29:44.863 回答
0

也尝试重定向标准输入并将 AutoFlush 应用于 StandardInput。下一个使用 StreamReader 读取流。

Process proces;
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "test.exe";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;

proces = Process.Start(psi);
proces.StandardInput.AutoFlush = true;
于 2013-10-29T12:47:51.530 回答
0

对不起,任何错误,我是巴西人,使用谷歌翻译来写这篇文章。

巧合的是,我也在做一个与 Virtualbox 的 VBoxManage 一起工作的程序。就我而言,我想转换一个虚拟磁盘。它也延迟和进度百分比

我设法通过创建一个运行程序的意志过程来做到这一点,并使用用户类“Dean North”来解决与此类似的另一个问题。使用线程运行VBoxManage很重要,否则无法处理获得的文本或查看进度。

O texto é muito grande pra eu adicionar quatro espaços antes de cada linha e repassar。

这些类替换了Process系统类。无需对您的代码进行任何更改,只需添加一个带有用户传递的文本的 arquivo.cs,Dean North而不是Process p = new Process()使用FixedProcess p = new FixedProcess ()

之后是我的代码:

private void BotaoParaTestes_Click(object sender, EventArgs e)
{
    string linha = @"clonehd " +
        "\"Z:\\Máquinas Virtuais\\Teste.vdi\" " +
        "\"C:\\Temp\\teste.vdi\" " +
        "--format VDI --variant Standard";

    Thread tarefa = new Thread(Executar);
    tarefa.Start(linha);
}
private void Executar(object Linha)
{
    FixedProcess fp = new FixedProcess ();
    fp.StartInfo.FileName = ItensEstaticos.VBox;
    fp.StartInfo.Arguments = Linha.ToString();
    fp.StartInfo.CreateNoWindow = true;
    fp.StartInfo.ErrorDialog = false;
    fp.StartInfo.RedirectStandardError = true;
    fp.StartInfo.RedirectStandardOutput = true;
    fp.StartInfo.UseShellExecute = false;
    fp.ErrorDataReceived += (sendingProcess, errorLine) => Escrita(errorLine.Data);
    fp.OutputDataReceived += (sendingProcess, dataLine) => Escrita(dataLine.Data);
    fp.Start();
    fp.BeginErrorReadLine();
    fp.BeginOutputReadLine();

    fp.WaitForExit();
}
private void Escrita(string Texto)
{
    if (!string.IsNullOrEmpty(Texto))
    {
        BeginInvoke(new MethodInvoker(delegate
        {
            this.Texto.Text += Texto;
        }));
    }
}

对我来说,仅在更改文本时才调用该事件,而不仅仅是在 VBoxManage 换行时调用。有时文本为空,然后像我在使用为控件获得的文本之前所做的那样放置一个检查结构。

于 2015-07-31T06:16:11.230 回答