如何:使用匿名管道在本地进程之间进行通信
更新:2007 年 11 月
匿名管道提供的功能比命名管道少,但它需要的系统开销也少。您可以使用匿名管道更加轻松地在本地计算机上进行进程间通信。不能使用匿名管道通过网络进行通信。
示例
下面的示例演示使用匿名管道将字符串从父进程发送到子进程的方式。此示例使用 Out 的 PipeDirection 值在父进程中创建一个 AnonymousPipeServerStream 对象。然后,父进程通过使用客户端句柄创建一个 AnonymousPipeClientStream 对象来创建一个子进程。该子进程的 In 值为 PipeDirection。
然后,父进程将用户提供的字符串发送给子进程。该字符串将显示在子进程中的控制台上。
下面的示例演示服务器进程。
Imports System
Imports System.IO
Imports System.IO.Pipes
Imports System.Diagnostics
Class PipeServer
Shared Sub Main()
Dim pipeClient As New Process()
pipeClient.StartInfo.FileName = "pipeClient.exe"
Using pipeServer As New AnonymousPipeServerStream( _
PipeDirection.Out, HandleInheritability.Inheritable)
Console.WriteLine("Current TransmissionMode: {0}.", _
pipeServer.TransmissionMode)
'Anonymous pipes do not support Message mode.
Try
Console.WriteLine("Setting ReadMode to 'Message'.")
pipeServer.ReadMode = PipeTransmissionMode.Message
Catch e As Exception
Console.WriteLine("EXCEPTION: {0}", e.Message)
End Try
' Pass the client process a handle to the server
pipeClient.StartInfo.Arguments = _
pipeServer.GetClientHandleAsString()
pipeClient.StartInfo.UseShellExecute = False
pipeClient.Start()
pipeServer.DisposeLocalCopyOfClientHandle()
Try
'Read user input and send that to the client process.
Using sw As New StreamWriter(pipeServer)
sw.AutoFlush = True
Console.Write("Enter text: ")
sw.WriteLine(Console.ReadLine())
End Using
Catch e As Exception
Console.WriteLine("ERROR: {0}", e.Message)
End Try
pipeClient.WaitForExit()
pipeClient.Close()
End Using
End Sub
End Class
using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;
class PipeServer
{
static void Main()
{
Process pipeClient = new Process();
pipeClient.StartInfo.FileName = "pipeClient.exe";
using (AnonymousPipeServerStream pipeServer =
new AnonymousPipeServerStream(PipeDirection.Out,
HandleInheritability.Inheritable))
{
Console.WriteLine("Current TransmissionMode: {0}.",
pipeServer.TransmissionMode);
// Anonymous pipes do not support Message mode.
try
{
Console.WriteLine("Setting ReadMode to \"Message\".");
pipeServer.ReadMode = PipeTransmissionMode.Message;
}
catch (NotSupportedException e)
{
Console.WriteLine("EXCEPTION: {0}", e.Message);
}
// Pass the client process a handle to the server.
pipeClient.StartInfo.Arguments =
pipeServer.GetClientHandleAsString();
pipeClient.StartInfo.UseShellExecute = false;
pipeClient.Start();
pipeServer.DisposeLocalCopyOfClientHandle();
try
{
// Read user input and send that to the client process.
using (StreamWriter sw = new StreamWriter(pipeServer))
{
sw.AutoFlush = true;
Console.Write("Enter text: ");
sw.WriteLine(Console.ReadLine());
}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
}
pipeClient.WaitForExit();
pipeClient.Close();
}
}
下面的示例演示客户端进程。服务器进程启动客户端进程,并为该进程提供一个客户端句柄。应该将从客户端代码得到的可执行文件命名为 pipeClient.exe 并在运行该服务器进程之前将其复制到服务器可执行文件所在的目录中。
Imports System
Imports System.IO
Imports System.IO.Pipes
Class PipeClient
Shared Sub Main(ByVal args As String())
If (args.Length > 0) Then
Using pipeClient As New AnonymousPipeClientStream( _
PipeDirection.In, args(0))
Console.WriteLine("Current TransmissionMode: {0}.", _
pipeClient.TransmissionMode)
' Anonymous Pipes do not support Message mode.
Try
Console.WriteLine("Setting ReadMode to 'Message'.")
pipeClient.ReadMode = PipeTransmissionMode.Message
Catch e As NotSupportedException
Console.WriteLine("EXCEPTION: {0}", e.Message)
End Try
Using sr As New StreamReader(pipeClient)
' Display the read text to the console
Dim temp As String
temp = sr.ReadLine()
While Not temp = Nothing
Console.WriteLine(temp)
temp = sr.ReadLine()
End While
End Using
End Using
End If
Console.Write("Press Enter to continue...")
Console.ReadLine()
End Sub
End Class
using System;
using System.IO;
using System.IO.Pipes;
class PipeClient
{
static void Main(string[] args)
{
if (args.Length > 0)
{
using (PipeStream pipeClient =
new AnonymousPipeClientStream(PipeDirection.In, args[0]))
{
Console.WriteLine("Current TransmissionMode: {0}.",
pipeClient.TransmissionMode);
// Anonymous Pipes do not support Message mode.
try
{
Console.WriteLine("Setting ReadMode to \"Message\".");
pipeClient.ReadMode = PipeTransmissionMode.Message;
}
catch (NotSupportedException e)
{
Console.WriteLine("EXCEPTION: {0}", e.Message);
}
using (StreamReader sr = new StreamReader(pipeClient))
{
// Display the read text to the console
string temp;
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine(temp);
}
}
}
}
Console.Write("Press Enter to continue...");
Console.ReadLine();
}
}