C # asynchronous echo of process running cmd command

Source: Internet
Author: User

The followingCodeExample of calling the CMD command for new process () and asynchronously returning the result to form:

 

 

 

 

 

 

 

 

 

 

 

 

 

[CSHARP] View plaincopy
    1. UsingSystem;
    2. UsingSystem. Collections. Generic;
    3. UsingSystem. componentmodel;
    4. UsingSystem. Data;
    5. UsingSystem. drawing;
    6. UsingSystem. LINQ;
    7. UsingSystem. text;
    8. UsingSystem. Windows. forms;
    9. UsingSystem. diagnostics;
    10. NamespaceCmdcallbackshow
    11. {
    12. // 1. Define the delegate
    13. Public Delegate VoidDelreadstdoutput (StringResult );
    14. Public Delegate VoidDelreaderroutput (StringResult );
    15. PublicPartialClassForm1: Form
    16. {
    17. // 2. Define a delegate event
    18. Public EventDelreadstdoutput readstdoutput;
    19. Public EventDelreaderroutput readerroutput;
    20. PublicForm1 ()
    21. {
    22. Initializecomponent ();
    23. Init ();
    24. }
    25. Private VoidInit ()
    26. {
    27. // 3. register the corresponding function to the delegate event
    28. Readstdoutput + =NewDelreadstdoutput (readstdoutputaction );
    29. Readerroutput + =NewDelreaderroutput (readerroutputaction );
    30. }
    31. Private VoidButton#click (ObjectSender, eventargs E)
    32. {
    33. // Configure the startup process as an example. In this example, run ping.exe.
    34. Realaction ("Ping.exe", Textbox1.text );
    35. }
    36. Private VoidRealaction (StringStartfilename,StringStartfilearg)
    37. {
    38. Process completed process =NewProcess ();
    39. Using process. startinfo. filename = startfilename;// Command
    40. Using process. startinfo. Arguments = startfilearg;// Parameters
    41. Using process. startinfo. createnowindow =True;// Do not create a new window
    42. Using process. startinfo. useshellexecute =False;
    43. Using process. startinfo. redirectstandardinput =True;// Redirect Input
    44. Using process. startinfo. redirectstandardoutput =True;// Redirect standard output
    45. Using process. startinfo. redirectstandarderror =True;// Redirect error output
    46. // Define process. startinfo. windowstyle = processwindowstyle. hidden;
    47. Using process. outputdatareceived + =NewDatareceivedeventhandler (p_outputdatareceived );
    48. Invalid process. errordatareceived + =NewDatareceivedeventhandler (p_errordatareceived );
    49. Export Process. enableraisingevents =True;// Enable the exited event
    50. Export Process. exited + =NewEventhandler (incluprocess_exited );// Register the Process Termination event
    51. Using process. Start ();
    52. Using process. beginoutputreadline ();
    53. Using process. beginerrorreadline ();
    54. // If comments are opened, run the command in synchronous mode. In this example, the exited event is executed asynchronously.
    55. // Wait process. waitforexit ();
    56. }
    57. Private VoidP_outputdatareceived (ObjectSender, datareceivedeventargs E)
    58. {
    59. If(E. Data! =Null)
    60. {
    61. // 4. asynchronous call, invoke required
    62. This. Invoke (readstdoutput,New Object[] {E. Data });
    63. }
    64. }
    65. Private VoidP_errordatareceived (ObjectSender, datareceivedeventargs E)
    66. {
    67. If(E. Data! =Null)
    68. {
    69. This. Invoke (readerroutput,New Object[] {E. Data });
    70. }
    71. }
    72. Private VoidReadstdoutputaction (StringResult)
    73. {
    74. This. Textboxshowstdret. appendtext (result +"\ R \ n");
    75. }
    76. Private VoidReaderroutputaction (StringResult)
    77. {
    78. This. Textboxshowerrret. appendtext (result +"\ R \ n");
    79. }
    80. Private VoidExport process_exited (ObjectSender, eventargs E)
    81. {
    82. // Triggered after execution ends
    83. }
    84. }
    85. }
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.