当前位置: 首页 > 编程语言 > C#

使用进程实时控制台输出重定向Share

时间:2023-04-11 10:50:01 C#

UsingProcessReal-timeConsoleOutputRedirection我正在使用VBOXMANAGE“导出”客户端。VBOXManage是一个控制台应用程序,可以控制来宾主机的来宾行为。由于导出命令是一个很长的过程,它返回这样的过程更新:0%...10%...20%...30%...100%我正在编写一个C#应用程序,它将调用VBOXManage使用过程。这是我的代码:ProcessVBOXProc=newProcess();VBOXProc.StartInfo.FileName=VBOXMANAGE;VBOXProc.StartInfo.Arguments=参数;VBOXProc.StartInfo.UseShellExecute=false;VBOXProc.StartInfo.CreateNoWindow=true;VBOXProc.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;VBOXProc.StartInfo.RedirectStandardError=true;VBOXProc.StartInfo.RedirectStandardOutput=true;VBOXProc.OutputDataReceived+=newDataReceivedEventHandler(VBOXProc_OutputDataReceived);VBOXProc.ErrorDataReceived+=newDataReceivedEventHandler(VBOXProc_ErrorDataReceived);VBOXProc.EnableRaisingEvents=true;VBOXProc.Start();VBOXProc.BeginOutputReadLine();VBOXProc.BeginErrorReadLine();VBOXProc.WaitForExit();这很好,除了每行读取输出。这意味着过程更新“0%...10%...20%...30%...100%”仅在实际过程完成后显示。有没有办法实时捕获控制台输出?谢谢!您可以使用所有标准Stream方法直接从StandadardOutput/Error读取进程,只需确保StartInfo.Redirectxxx设置为true。varp=newProcess()p.StartInfo.UseShellExecute=false;//不确定这是否绝对需要p.StartInfo.RedirectStandardOuput=true;....做{Thread.Sleep(nnn);Console.Out.Write(p.StandardOutput.ReadToEnd());}while(!p.HasExited);//捕获重定向标准输出中的任何剩余内容Console.Out.Write(p.StandardOutput.ReadToEnd());以上将子进程的输出发送回应用程序的标准输出。您可以使用p.StandardOutput.Read(char[],int,int)读取特定大小的块,或者使用p.StadardOutput.BaseStream.BeginRead(…)异步读取。所有相同的方法都可用于StandardError。在循环中,hibernate释放处理器以执行其他任务,并允许一些数据累积在缓冲区中。如果睡眠周期过长,缓冲区溢出,执行过程中的一些输出会丢失。如果睡眠时间太短,许多CPU周期将花费在读取和清空缓冲区上。这对我有用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.BeginErrorReadLine();process.BeginOutputReadLine();过程.WaitForExit();error.AppendLine()和SetMessage()是我的方法。尝试重定向标准输入,并将AutoFlush应用于StandardInput。接下来使用StreamReader读取流。工艺流程;ProcessStartInfopsi=newProcessStartInfo();psi.FileName="test.exe";psi.UseShellExecute=false;psi.CreateNoWindow=true;psi.RedirectStandardOutput=true;psi.RedirectStandardInput=true;(psi);程序.StandardInput.AutoFlush=true;对于任何错误,我深表歉意,我是巴西人,使用谷歌翻译来写这篇文章。巧合的是,我也在开发一个与Virtualbox的VBoxManage一起工作的程序。就我而言,除其他外,我想转换一个虚拟磁盘。它也被延迟了,也有一定的进展我设法通过创建一个运行程序的意志流来做到这一点,并使用来自另一个与此类似的问题的用户类“DeanNorth”。使用线程运行VBoxManage很重要,否则您无法处理获取的文本或查看进度。Otextoémuitograndepraeuadicionarquatroespa?osantesdecadalinhaerepassar。这些类取代了Process系统类。无需对代码进行任何更改,只需添加一个arquivo.cs,其中包含用户DeanNorth传递的文本,而不是Processp=newProcess()使用FixedProcessp=newFixedProcess()后跟我的代码:privatevoidBotaoParaTestes_Click(objectsender,EventArgse){stringlinha=@"clonehd"+""Z:\MáquinasVirtuais\Teste.vdi""+""C:\Temp\teste.vdi""+"--格式化VDI--变体标准”;线程tarefa=newThread(Executar);tarefa.Start(linha);}privatevoidExecutar(objectLinha){FixedProcessfp=newFixedProcess();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.开始();fp.BeginErrorReadLine();fp.BeginOutputReadLine();fp.WaitForExit();}privatevoidEscrita(stringTexto){if(!string.IsNullOrEmpty(Texto)){BeginInvoke(newMethodInvoker(delegate{this.Texto.Text+=Texto;}));对我来说,事件仅在文本更改时调用,而不仅仅是当VBoxManage转到新行时,有时文本为空,然后在使用为控件获取的文本之前放置一个检查结构。以上就是C#学习教程:使用Process实时控制台输出重定向,分享全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。涉及侵权,请点击维权联系管理员删除。如需转载请注明出处: