请问什么是数据管道,在c#中是怎么具体应用的?

来源:百度知道 编辑:UC知道 时间:2024/05/13 14:18:10
请问什么是数据管道,在c#中是怎么具体应用的?最好给个简单的小例子,谢谢了。给分

操作系统中负责线程间通讯的东西叫管道。

管道(pipe)是进程用来通讯的共享内存区域。一个进程往管道中写入信息,而其它的进程可以从管道中读出信息。如其名,管道是进程间数据交流的通道。

管道技术一般采用Window API来实现,但C#本身方便的进程线程机制使工作变得简单.
首先,我们可以通过设置Process类,获取输出接口,代码如下:

Process proc = new Process();
proc .StartInfo.FileName = strScript;
proc .StartInfo.WorkingDirectory = strDirectory;
proc .StartInfo.CreateNoWindow = true;
proc .StartInfo.UseShellExecute = false;
proc .StartInfo.RedirectStandardOutput = true;
proc .Start();

然后设置线程连续读取输出的字符串:
eventOutput = new AutoResetEvent(false);
AutoResetEvent[] events = new AutoResetEvent[1];
events[0] = m_eventOutput;

m_threadOutput = new Thread( new ThreadStart( DisplayOutput ) );
m_threadOutput.Start();
WaitHandle.WaitAll( events );

线程函数如下:
private void DisplayOutput()
{
while ( m_procScript != null && !m_procScript.HasExited )
{
string strLine = null;
while ( ( strLine = m_