在.NET程序中使用PIPE(管道技术)
来源:互联网 发布:adp数据对黄金的 编辑:程序博客网 时间:2024/05/22 17:25
.NET框架被设计为禁止内存共享的, 但是好在FCL依然提供了一些类, 使得我们还是可以使用管道技术.
PIPE, 管道, 他的原理其实就是应用程序的输入输出重定向. 比如原来需要显示在屏幕上的东西不让他传送到屏幕, 而是让你的程序处理/分析, 再显示出结果. 下面是我刚才写的利用cmd.exe执行ping操作, 把结果在一个WinForm的RichTextBox中显示出的例子.
[截图]
[代码]
using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
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()
{
InitializeComponent();
Thread t = new Thread(new ThreadStart(Run));
tbResult.Text = "Processing...";
t.Start();
}
private void Run()
{
p = new Process();
p.StartInfo.FileName = "cmd.exe";
// 这里是关键点,不用Shell启动/重定向输入/重定向输出/不显示窗口
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("ping 127.0.0.1");// 向cmd.exe输入command
p.StandardInput.WriteLine("exit");
p.WaitForExit(60000);
string s = p.StandardOutput.ReadToEnd();// 得到cmd.exe的输出
p.Close();
tbResult.Text = s.Replace("/r","").Replace("/n","/r/n");
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
[STAThread]
static void Main()
{
Application.Run(new Sample());
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
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.Anchor = ((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.btnExit.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.Controls.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();
}
}
}
// 在Windows2003 Server, .NET Framework1.1.4322平台运行通过, VS.NET 2003调试通过.
怎么样, 很简单吧? 要是还不懂自己调试调试代码就明白啦.
关于Windows的PIPE技术本身, 可以看看这两篇文章:
Windows管道技术简述
Windows系列操作系统对线程间通信的支持
- 在.NET程序中使用PIPE(管道技术)
- 在.NET程序中使用PIPE(管道技术)
- Ionic2中使用管道(Pipe)
- pipe管道的使用
- angular2--pipe管道使用
- 用管道(pipe)使消息队列通知(mq_notify)可以在Select和poll中使用
- 用管道(pipe)使消息队列通知(mq_notify)可以在Select和poll中使用
- 在VC中使用Windows管道技术编程
- 在VC中使用Windows管道技术编程
- 在VC中使用Windows管道技术编程
- Linux管道pipe使用实例
- 使用匿名管道pipe例子
- 管道Pipe的初次使用
- 无名管道pipe的使用
- 无名管道pipe的使用
- 无名管道pipe的使用
- 使用管道Pipe 完成数据传输
- 在.net中使用语音技术
- 修改JBoss服务器端口
- Registry Workshop Version 2.6.1
- RapidCheck 0.3 + Rapidshare Free Account Notifier 1.0
- 累啊。
- 深入分析DWREngine.endBatch
- 在.NET程序中使用PIPE(管道技术)
- 开源:.NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现
- spring 配置实例4(使用DefaultAdvisorAutoProxyCreator 自动代理,比较省时省力的做法)
- 我喜欢Symbian
- 开源:.NET环境下有关打印页面设置、打印机设置、打印预览对话框的应用
- 中印软件工程师之比较
- Symbian编程VC开发环境设置
- Symbian内存管理的二十二条军规
- Symbian入门指南-接触篇