Use PIPE (pipe technology) in .NET program

zhaozj2021-02-16  52

Use PIPE (pipe technology) in .NET program

University of Electronic Science and Technology Xia Yang

I don't know what to do in these days. I have been influenced by friends. I have read "New Tianlong Babu", I am very touched, so I plan to do something good. =) About PIPE applications, the principle is very simple, so I wrote this article, maybe It is necessary to be generous by the peers, but I hope that I don't know some help from the newcomer.

The .NET framework is designed to ban memory sharing, but it is good to provide some classes in FCL, making us still use pipeline technology .pipe, pipeline, his principle is actually an input and output redirection of the application. For example, it is necessary to display On the screen does not let him transfer to the screen, but let your program handle / analyze, then display the result. Below is the use of cmd.exe to perform ping operations, display the result in a WinForm's RichtextBox Example. [Screenshot]

[Code] Using system.diagnostics; using system.drawing; using system.COMPONENTMODEL; using system.threading; using system.windows.form;

Namespace sunmast.sample.pipe {public class sample: system.windows.Forms.form {private process p;

Private system.windows.forms.richtextbox tbresult; private system.windows.forms.button btnexit; private system.windows.Forms.Button btnrefresh;

Private system.componentmodel.Container Components = NULL;

Public Sample () {INITIALIZECMPONENT (); thread t = new thread (new threadstart (run)); tbresult.text = "processing ..."; t.start ();}

Private void run () {p = new process (); p.startinfo.filename = "cmd.exe";

// This is a key point, no shell start / redirection input / redirection output / not display window P.StartInfo.uSeshellexecute = false; p.startinfo.redirectStandIndInput = true; p.startinfo.redirectstandoutput = true; p.StartInfo. Createnowindow = True;

P.Start (); p.standardinput.writeline ("ping 127.0.0.1"); // Enter Command P.standardInput.writeline ("exit") to cmd.exe; p.WaitForexit (60000); string s = P .Standardoutput.readtoend (); // Get the output of cmd.exe ();

TBRESULT.TEXT = S.Replace ("/ r", ""). Replace ("/ n", "/ r / n");}

Protected Override Void Dispose (bool disposing) {if (disponents! = null) {components.dispose ();}} Base.dispose ();} [statHread] static void main () {Application.run NEW SAMPLE ());

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.tbResult = new System.Windows.Forms.RichTextBox (); this.btnExit = new System.Windows.Forms.Button (); this.btnRefresh = new System. Windows.Forms.Button (); this.suspendlayout (); // // tbresult // this.tbresult.ancy = ((System.Windows.Forms.Anchorstyles) (((System.Windows.Forms.Anchorstyles.top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.tbResult.BackColor = System.Drawing.SystemColors.Info; This.tbresult.location = new system.drawing.point (8, 8); this.tbresult.name = "TBRESULT"; this.tbresult.readonly = true; this.tbresult.size = new system.drawing.size (376 , 424); this.tbresult.tabindex = 0; this.tbresult.text = ""; // // btnexit // this.btnexit.Anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.) Forms.anchorstyles.bottom | System.windows.Forms.Anchorstyles.right))); this.btn Exit.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btnExit.Location = new System.Drawing.Point (320, 440); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System .Drawing.size (64, 24); this.btnexit.tabindex = 1; this.btnexit.text = "exit"; this.btnexit.click = new system.eventhandler (this.btnexit_click); // // btnrefresh // this.btnRefresh.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btnRefresh.Location = new System.drawing.point (248, 440); this.btnrefresh.name = "

btnRefresh "; this.btnRefresh.Size = new System.Drawing.Size (64, 24); this.btnRefresh.TabIndex = 2; this.btnRefresh.Text =" Refresh "; this.btnRefresh.Click = new System.EventHandler (this.btnRefresh_Click); // // Sample // this.AcceptButton = this.btnRefresh; this.AutoScaleBaseSize = new System.Drawing.Size (6, 14); this.CancelButton = this.btnExit; this.ClientSize = new System.drawing.size (392, 471); this.btrs.add (this.btnrefresh); this.controls.add (this.btnexit); this.controls.add (this.tbresult); this.name = "Sample "; This.text =" Sample "; this.ResumeLayout (false);} #ENDREGION

Private void btnexit_click (object sender, system.eventargs e) {Application.exit ();

Private void btnrefresh_click (object sender, system.eventargs e) {this.Run ();}}} // In Windows2003 Server, .NET Framework1.1.4322 platform runs, vs.net 2003 debugging.

How is it, very simple? If you don't understand your debug debug code, you will understand. About Windows PIPE technology itself, you can take a look at these two articles: Windows pipeline technology briefly describes the support of the WINDOWS series of operating systems

My blog: http://blog.sunmast.com/

转载请注明原文地址:https://www.9cbs.com/read-24263.html

New Post(0)