将命令提示符输出重定向到Winform时遇到Process类的问题 [英] Having trouble with Process class while redirecting command prompt output to winform

查看:74
本文介绍了将命令提示符输出重定向到Winform时遇到Process类的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时在市场上寻找选项卡式命令提示符应用程序,因为当我想到自己创建自己的可能性时,我厌倦了多个命令提示符窗口使我的一个桌面屏幕混乱。虽然我知道它不会像独立产品那样出色,但是我认为创建自己的产品只是为了更加熟悉 System.Diagnostic类(Process,ProcessStartInfo等)是一个很好的练习。

I was in the market looking for a "tabbed" command prompt application, as I was sick of having multiple command prompt windows cluttering one of my desktop screens, when I thought of a possibility of creating my own. While I understand that it won't be as great as a standalone product, but I think it will be a good exercise to create my own just to get more familiar with the "System.Diagnostic" classes (Process, ProcessStartInfo, etc.) that I've never really played around with before.

我几乎刚刚创建了一个准系统winforms应用,该应用具有2个选项卡(包含richtextfields),一个textfield(输入命令)和一个按钮(实际运行命令)。

I've pretty much just created a barebones winforms app that has 2 tabs (that contain richtextfields), a textfield (to enter the command) and a button (to actually run the command).

我发现了许多文章,展示了如何运行命令,但实际上我遇到了问题执行命令并将结果返回到richtextbox。这是我根据目前知道的信息创建的方法:
(根据之前的答案进行了更新)

I found various posts that show how to run a command, but I'm having problems actually executing a command and returning the results to the richtextbox. Here is a method I created based on what info I know for now: (Updated base on previous answers)

public void GetConsoleOuput(string command)
{
  string outputString;

  ProcessStartInfo startupInfo = new ProcessStartInfo()
  startInfo.FileName = "cmd.exe";
  startInfo.RedirectStandardOuput = true;
  startInfo.WindowStyle = ProcessWindowStyle.Hidden:
  startInfo.UseShellExecute = false;

  startInfo.Arguments("/C " + command);

  Process process = new Process()
  process.StartInfo = startInfo;
  process.OutputDataReceived += new DataReceivedEventHandler(AppendRichBoxText);

  process.Start()
  process.BeginOutputReadLine();

  process.WaitForExit();
  process.Close();
}

public void AppendRichBoxTet(object sender, DataReceivedEventArgs args)
{
  string outputString = args.Data;

  // need to have the richTextBox updated using it's own thread
  richTextBox.BeginInvoke( /* not sure what to put inside here */);
}

此方法的使用是将输出文本附加到richtextbox上

The use of this method would be to keep appending the output text to the richtextbox.

这时,我仍然停留在如何执行BeginInvoke方法上,以便该richTextBox可以在自己的线程上对其文本进行更新。

At this point, I'm stuck on how to execute the BeginInvoke method so that this richTextBox will have it's text updated on it's own thread.

推荐答案

是的,可以。可以使用事件驱动的输出重定向API来代替使用 ReadToEnd (它将阻塞直到过程完成):

Yes, you can. Instead of using ReadToEnd (which will block until the process finishes) you can use the event-driven output redirection API:

  • Set RedirectStandardOutput as you are doing already
  • Start the process
  • Call BeginOutputReadLine
  • Subscribe to the OutputDataReceived event and use Control.BeginInvoke to marshal back to the UI thread to append to the RichTextBox.

有一个示例在 BeginOutputReadLine 文档中。

编辑:对于 Control.BeginInvoke ,这可能是最简单的解决方案:

For the Control.BeginInvoke, this would probably be the simplest solution:

public void AppendRichBoxTet(object sender, DataReceivedEventArgs args)
{
  string outputString = args.Data;
  MethodInvoker append = () => richTextBox.AppendText(outputString);
  richTextBox.BeginInvoke(append);
}

这篇关于将命令提示符输出重定向到Winform时遇到Process类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆