The following code calls the cmd command for New Process () and asynchronously echoes the result to the example of the form:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Diagnostics;
Namespace Cmdcallbackshow
{
1. Defining delegates
public delegate void Delreadstdoutput (string result);
public delegate void Delreaderroutput (string result);
Public partial class Form1:form {//2. Defining delegate Events public event Delreadstdoutput Readstdoutput; public event Delreaderroutput Readerroutput; Public Form1 () {InitializeComponent (); Init (); } private void Init () {//3. Register the corresponding function in the delegate event Readstdoutput + = new Delreadstdoutput (Readstdoutputa ction); Readerroutput + = new Delreaderroutput (readerroutputaction); The private void Button1_Click (object sender, EventArgs e) {//starts the process to execute the appropriate command, in this case the execution of Ping.exe as an example Real Action ("Ping.exe", TextBox1.Text); private void Realaction (String startfilename, String startfilearg) {Process cmdprocess = new P Rocess (); CmdProcess.StartInfo.FileName = StartFileName; Command CmdProcess.StartInfo.Arguments = Startfilearg; Parameter CmdProcess.StartInfo.CreateNoWindow = true; Do not create a new window CmdProcess.StartInfo.UseShellExecute = false; CmdProcess.StartInfo.RedirectStandardInput = true; REDIRECT Input CmdProcess.StartInfo.RedirectStandardOutput = true; REDIRECT Standard output CmdProcess.StartInfo.RedirectStandardError = true; REDIRECT Error output//cmdprocess.startinfo.windowstyle = Processwindowstyle.hidden; cmdprocess.outputdatareceived + = new Datareceivedeventhandler (p_outputdatareceived); cmdprocess.errordatareceived + = new Datareceivedeventhandler (p_errordatareceived); Cmdprocess.enableraisingevents = true; Enable Exited event cmdprocess.exited + = new EventHandler (cmdprocess_exited); Registration process End Event Cmdprocess.start (); Cmdprocess.beginoutputreadline (); Cmdprocess.beginerrorreadline (); If you open a comment, the command executes synchronously, and this example executes asynchronously with the exited event. Cmdprocess.waitforexit (); private void P_outputdatareceived (object sender, Datareceivedeventargs e) {if (e.data! = null) {//4. Asynchronous invocation, requires InvokE this. Invoke (Readstdoutput, new object[] {e.data}); }} private void P_errordatareceived (object sender, Datareceivedeventargs e) {if (e.data! = null) {this. Invoke (Readerroutput, new object[] {e.data}); }} private void Readstdoutputaction (string result) {This.textBoxShowStdRet.AppendText (result +) \ r \ n "); } private void Readerroutputaction (string result) {This.textBoxShowErrRet.AppendText (result + "\ r \ n"); private void Cmdprocess_exited (object sender, EventArgs e) {//Trigger after execution}}
}
To C # process run asynchronous echo of cmd command