在现代软件开发中,C#进程间通信(IPC)是一项非常重要的技术。当多个应用程序或同一程序的多个实例需要共享数据、协调任务时,就需要用到 IPC。而管道(Pipe)是 Windows 系统中最常用、最高效的 IPC 机制之一。
本文将带你从零开始学习 C# 中如何使用命名管道和匿名管道实现进程间通信,无论你是刚接触 C# 的小白,还是有一定经验的开发者,都能轻松上手。
管道是一种用于在进程之间传递数据的通信机制。它就像一根“水管”,一端写入数据,另一端读取数据。C# 中主要支持两种管道:
匿名管道常用于主程序启动子进程并与其交换数据。下面是一个简单的例子:父进程向子进程发送一条消息。
using System;using System.Diagnostics;using System.IO;using System.IO.Pipes;class ParentProcess{ static void Main() { // 创建匿名管道 using (var pipeServer = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable)) { // 启动子进程,并将管道客户端句柄传给它 var psi = new ProcessStartInfo { FileName = "Child.exe", Arguments = pipeServer.GetClientHandleAsString(), UseShellExecute = false }; Process child = Process.Start(psi); // 写入数据 using (StreamWriter writer = new StreamWriter(pipeServer)) { writer.WriteLine("Hello from parent process!"); writer.Flush(); } child.WaitForExit(); } }} 子进程代码(Child.cs):
using System;using System.IO;using System.IO.Pipes;class ChildProcess{ static void Main(string[] args) { if (args.Length > 0) { // 使用传入的句柄创建客户端流 using (var pipeClient = new AnonymousPipeClientStream(PipeDirection.In, args[0])) using (StreamReader reader = new StreamReader(pipeClient)) { string message = reader.ReadLine(); Console.WriteLine($"Received: {message}"); } } }} 命名管道更灵活,适用于两个独立运行的程序。我们创建一个服务器端监听管道,客户端连接并发送消息。
服务器端代码(NamedPipeServer.cs):
using System;using System.IO;using System.IO.Pipes;using System.Text;class NamedPipeServer{ static void Main() { const string pipeName = "MyCSharpPipe"; Console.WriteLine("等待客户端连接..."); using (var server = new NamedPipeServerStream(pipeName, PipeDirection.InOut)) { server.WaitForConnection(); Console.WriteLine("客户端已连接!"); using (StreamReader reader = new StreamReader(server)) using (StreamWriter writer = new StreamWriter(server) { AutoFlush = true }) { string input = reader.ReadLine(); Console.WriteLine($"收到消息: {input}"); writer.WriteLine($"服务器回复: 收到你的消息 '{input}'"); } } Console.WriteLine("通信结束。"); }} 客户端代码(NamedPipeClient.cs):
using System;using System.IO;using System.IO.Pipes;class NamedPipeClient{ static void Main() { const string pipeName = "MyCSharpPipe"; using (var client = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut)) { Console.WriteLine("正在连接服务器..."); client.Connect(); using (StreamWriter writer = new StreamWriter(client) { AutoFlush = true }) using (StreamReader reader = new StreamReader(client)) { writer.WriteLine("你好,服务器!"); string response = reader.ReadLine(); Console.WriteLine($"服务器回复: {response}"); } } }} System.IO.Pipes 命名空间,使用 StreamReader/StreamWriter 可简化文本传输。- 命名管道名称必须全局唯一,建议加上公司或项目前缀避免冲突。
- 管道默认仅限本地通信,若需网络通信,需配置 Windows 的“文件和打印机共享”服务。
- 生产环境中应添加异常处理(如 try-catch)和超时机制,防止死锁。
通过本教程,你已经掌握了 IPC管道编程 在 C# 中的核心用法。现在可以尝试构建自己的多进程应用了!
本文由主机测评网于2025-12-06发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025123954.html