To C # process run asynchronous echo of cmd command

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.