Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

c# - Having trouble with Process class while redirecting command prompt output to winform

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.

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

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 */);
}

The use of this method would be to keep appending the output text to the 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

There's an example in the BeginOutputReadLine documentation.

EDIT: 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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...