The followingCodeExample of calling the CMD command for new process () and asynchronously returning the result to form:
[CSHARP] View plaincopy
-
- UsingSystem;
-
- UsingSystem. Collections. Generic;
-
- UsingSystem. componentmodel;
- UsingSystem. Data;
-
- UsingSystem. drawing;
-
- UsingSystem. LINQ;
-
- UsingSystem. text;
-
- UsingSystem. Windows. forms;
-
- UsingSystem. diagnostics;
-
-
- NamespaceCmdcallbackshow
-
- {
- // 1. Define the delegate
-
- Public Delegate VoidDelreadstdoutput (StringResult );
-
- Public Delegate VoidDelreaderroutput (StringResult );
-
- PublicPartialClassForm1: Form
-
- {
-
- // 2. Define a delegate event
-
- Public EventDelreadstdoutput readstdoutput;
-
- Public EventDelreaderroutput readerroutput;
-
- PublicForm1 ()
-
- {
-
- Initializecomponent ();
-
- Init ();
-
- }
-
-
- Private VoidInit ()
- {
-
- // 3. register the corresponding function to the delegate event
-
- Readstdoutput + =NewDelreadstdoutput (readstdoutputaction );
-
- Readerroutput + =NewDelreaderroutput (readerroutputaction );
-
- }
-
- Private VoidButton#click (ObjectSender, eventargs E)
-
- {
-
- // Configure the startup process as an example. In this example, run ping.exe.
-
- Realaction ("Ping.exe", Textbox1.text );
-
- }
-
- Private VoidRealaction (StringStartfilename,StringStartfilearg)
-
- {
-
- Process completed process =NewProcess ();
- Using process. startinfo. filename = startfilename;// Command
-
- Using process. startinfo. Arguments = startfilearg;// Parameters
-
-
- Using process. startinfo. createnowindow =True;// Do not create a new window
- Using process. startinfo. useshellexecute =False;
-
- Using process. startinfo. redirectstandardinput =True;// Redirect Input
-
- Using process. startinfo. redirectstandardoutput =True;// Redirect standard output
- Using process. startinfo. redirectstandarderror =True;// Redirect error output
-
- // Define process. startinfo. windowstyle = processwindowstyle. hidden;
-
-
- Using process. outputdatareceived + =NewDatareceivedeventhandler (p_outputdatareceived );
- Invalid process. errordatareceived + =NewDatareceivedeventhandler (p_errordatareceived );
-
-
- Export Process. enableraisingevents =True;// Enable the exited event
- Export Process. exited + =NewEventhandler (incluprocess_exited );// Register the Process Termination event
-
-
- Using process. Start ();
-
- Using process. beginoutputreadline ();
-
- Using process. beginerrorreadline ();
-
- // If comments are opened, run the command in synchronous mode. In this example, the exited event is executed asynchronously.
-
- // Wait process. waitforexit ();
-
- }
-
-
- Private VoidP_outputdatareceived (ObjectSender, datareceivedeventargs E)
- {
-
- If(E. Data! =Null)
-
- {
-
- // 4. asynchronous call, invoke required
- This. Invoke (readstdoutput,New Object[] {E. Data });
-
- }
-
- }
-
-
- Private VoidP_errordatareceived (ObjectSender, datareceivedeventargs E)
- {
-
- If(E. Data! =Null)
-
- {
-
- This. Invoke (readerroutput,New Object[] {E. Data });
-
- }
- }
-
-
- Private VoidReadstdoutputaction (StringResult)
-
- {
-
- This. Textboxshowstdret. appendtext (result +"\ R \ n");
-
- }
-
- Private VoidReaderroutputaction (StringResult)
-
- {
-
- This. Textboxshowerrret. appendtext (result +"\ R \ n");
-
- }
-
- Private VoidExport process_exited (ObjectSender, eventargs E)
-
- {
-
- // Triggered after execution ends
-
- }
-
- }
-
- }