C# Process运行cmd命令的异步回显

以下的代码为new Process() 调用cmd命令,并将结果异步回显到Form的例子:

 
 
 
 
 
 
 
 
 
 
 
 
 

[csharp] view plain copy

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. namespace CmdCallbackShow
  11. {
  12.     // 1.定义委托
  13.     public delegate void DelReadStdOutput(string result);
  14.     public delegate void DelReadErrOutput(string result);
  15.     public partial class Form1 : Form
  16.     {
  17.         // 2.定义委托事件
  18.         public event DelReadStdOutput ReadStdOutput;
  19.         public event DelReadErrOutput ReadErrOutput;
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.             Init();
  24.         }
  25.         private void Init()
  26.         {
  27.             //3.将相应函数注册到委托事件中
  28.             ReadStdOutput += new DelReadStdOutput(ReadStdOutputAction);
  29.             ReadErrOutput += new DelReadErrOutput(ReadErrOutputAction);
  30.         }
  31.         private void button1_Click(object sender, EventArgs e)
  32.         {
  33.             // 启动进程执行相应命令,此例中以执行ping.exe为例
  34.             RealAction(“ping.exe”, textBox1.Text);
  35.         }
  36.         private void RealAction(string StartFileName, string StartFileArg)
  37.         {
  38.             Process CmdProcess = new Process();
  39.             CmdProcess.StartInfo.FileName = StartFileName;      // 命令
  40.             CmdProcess.StartInfo.Arguments = StartFileArg;      // 参数
  41.             CmdProcess.StartInfo.CreateNoWindow = true;         // 不创建新窗口
  42.             CmdProcess.StartInfo.UseShellExecute = false;
  43.             CmdProcess.StartInfo.RedirectStandardInput = true;  // 重定向输入
  44.             CmdProcess.StartInfo.RedirectStandardOutput = true// 重定向标准输出
  45.             CmdProcess.StartInfo.RedirectStandardError = true;  // 重定向错误输出
  46.             //CmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  47.             CmdProcess.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
  48.             CmdProcess.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
  49.             CmdProcess.EnableRaisingEvents = true;                      // 启用Exited事件
  50.             CmdProcess.Exited += new EventHandler(CmdProcess_Exited);   // 注册进程结束事件
  51.             CmdProcess.Start();
  52.             CmdProcess.BeginOutputReadLine();
  53.             CmdProcess.BeginErrorReadLine();
  54.             // 如果打开注释,则以同步方式执行命令,此例子中用Exited事件异步执行。
  55.             // CmdProcess.WaitForExit();     
  56.         }
  57.         private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
  58.         {
  59.             if (e.Data != null)
  60.             {
  61.                 // 4. 异步调用,需要invoke
  62.                 this.Invoke(ReadStdOutput, new object[] { e.Data });
  63.             }
  64.         }
  65.         private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
  66.         {
  67.             if (e.Data != null)
  68.             {
  69.                 this.Invoke(ReadErrOutput, new object[] { e.Data });
  70.             }
  71.         }
  72.         private void ReadStdOutputAction(string result)
  73.         {
  74.             this.textBoxShowStdRet.AppendText(result + “\r\n”);
  75.         }
  76.         private void ReadErrOutputAction(string result)
  77.         {
  78.             this.textBoxShowErrRet.AppendText(result + “\r\n”);
  79.         }
  80.         private void CmdProcess_Exited(object sender, EventArgs e)
  81.         {
  82.             // 执行结束后触发
  83.         }
  84.     }
  85. }
Article By :

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注