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.   
    11. namespace CmdCallbackShow  
    12. {  
    13.     // 1.定義委托  
    14.     public delegate void DelReadStdOutput(string result);  
    15.     public delegate void DelReadErrOutput(string result);  
    16.   
    17.     public partial class Form1 : Form  
    18.     {  
    19.         // 2.定義委托事件  
    20.         public event DelReadStdOutput ReadStdOutput;  
    21.         public event DelReadErrOutput ReadErrOutput;  
    22.   
    23.         public Form1()  
    24.         {  
    25.             InitializeComponent();  
    26.             Init();  
    27.         }  
    28.   
    29.         private void Init()  
    30.         {  
    31.             //3.將相應函數注冊到委托事件中  
    32.             ReadStdOutput += new DelReadStdOutput(ReadStdOutputAction);  
    33.             ReadErrOutput += new DelReadErrOutput(ReadErrOutputAction);  
    34.         }  
    35.   
    36.         private void button1_Click(object sender, EventArgs e)  
    37.         {  
    38.             // 啟動進程執行相應命令,此例中以執行ping.exe為例  
    39.             RealAction("ping.exe", textBox1.Text);  
    40.         }  
    41.           
    42.         private void RealAction(string StartFileName, string StartFileArg)  
    43.         {  
    44.             Process CmdProcess = new Process();  
    45.             CmdProcess.StartInfo.FileName = StartFileName;      // 命令  
    46.             CmdProcess.StartInfo.Arguments = StartFileArg;      // 參數  
    47.   
    48.             CmdProcess.StartInfo.CreateNoWindow = true;         // 不創建新窗口  
    49.             CmdProcess.StartInfo.UseShellExecute = false;  
    50.             CmdProcess.StartInfo.RedirectStandardInput = true;  // 重定向輸入  
    51.             CmdProcess.StartInfo.RedirectStandardOutput = true// 重定向標准輸出  
    52.             CmdProcess.StartInfo.RedirectStandardError = true;  // 重定向錯誤輸出  
    53.             //CmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;  
    54.   
    55.             CmdProcess.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);  
    56.             CmdProcess.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);  
    57.   
    58.             CmdProcess.EnableRaisingEvents = true;                      // 啟用Exited事件  
    59.             CmdProcess.Exited += new EventHandler(CmdProcess_Exited);   // 注冊進程結束事件  
    60.   
    61.             CmdProcess.Start();  
    62.             CmdProcess.BeginOutputReadLine();  
    63.             CmdProcess.BeginErrorReadLine();  
    64.   
    65.             // 如果打開注釋,則以同步方式執行命令,此例子中用Exited事件異步執行。  
    66.             // CmdProcess.WaitForExit();       
    67.         }  
    68.   
    69.         private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)  
    70.         {  
    71.             if (e.Data != null)  
    72.             {  
    73.                 // 4. 異步調用,需要invoke  
    74.                 this.Invoke(ReadStdOutput, new object[] { e.Data });  
    75.             }  
    76.         }  
    77.   
    78.         private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)  
    79.         {  
    80.             if (e.Data != null)  
    81.             {  
    82.                 this.Invoke(ReadErrOutput, new object[] { e.Data });  
    83.             }  
    84.         }  
    85.   
    86.         private void ReadStdOutputAction(string result)  
    87.         {  
    88.             this.textBoxShowStdRet.AppendText(result + "\r\n");  
    89.         }  
    90.   
    91.         private void ReadErrOutputAction(string result)  
    92.         {  
    93.             this.textBoxShowErrRet.AppendText(result + "\r\n");  
    94.         }  
    95.   
    96.         private void CmdProcess_Exited(object sender, EventArgs e)  
    97.         {  
    98.             // 執行結束后觸發  
    99.         }  
    100.     }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM