如何:使用匿名管道在本地进程之间进行通信(C#)

 我来答
小赖6yC1
2013-11-09 · 超过49用户采纳过TA的回答
知道答主
回答量:97
采纳率:100%
帮助的人:90.4万
展开全部
匿名管道提供的功能比命名管道少,但它需要的系统开销也少。您可以使用匿名管道更加轻松地在本地计算机上进行如轮大进程间通信。不能使用匿名管道通过网络进行通信。示例下面的示例演示使用匿名管道将字符串从父进程发送到子进程的方式。此示例渣竖使用Out 的PipeDirection值在父进程中创建一个AnonymousPipeServerStream对象。然后,父进程通过使用客户端句柄创建一个AnonymousPipeClientStream对象来创建一个子进程。该子进程的In 值为PipeDirection。然后,父进程将用户提供的字符串发送给子进程。该字符串将显示在子进程中的控制台上。using System;using System.IO;using System.IO.Pipes;using System.Diagnostics; class PipeServer{ staticvoid Main() { Process pipeClient = new Process(); pipeClient.StartInfo.FileName = "pipeClient.exe"; using (AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable)) { // Show that anonymous pipes do not support Message mode. try { Console.WriteLine("[SERVER] Setting ReadMode to \"Message\"."); pipeServer.ReadMode = PipeTransmissionMode.Message; } catch (NotSupportedException e) { Console.WriteLine("桐困[SERVER] Exception:\n {0}", e.Message); } Console.WriteLine("[SERVER] Current TransmissionMode: {0}.", pipeServer.TransmissionMode); // 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; // Send a 'sync message' and wait for client to receive it. sw.WriteLine("SYNC"); pipeServer.WaitForPipeDrain(); // Send the console input to the client process. Console.Write("[SERVER] Enter text: "); sw.WriteLine(Console.ReadLine()); } } // Catch the IOException that is raised if the pipe is broken // or disconnected. catch (IOException e) { Console.WriteLine("[SERVER] Error: {0}", e.Message); } } pipeClient.WaitForExit(); pipeClient.Close(); Console.WriteLine("[SERVER] Client quit. Server terminating."); }} 下面的示例演示客户端进程。using System;using System.IO;using System.IO.Pipes; class PipeClient{ staticvoid Main(string[] args) { if (args.Length > 0) { using (PipeStream pipeClient = new AnonymousPipeClientStream(PipeDirection.In, args[0])) { // Show that anonymous Pipes do not support Message mode. try { Console.WriteLine("[CLIENT] Setting ReadMode to \"Message\"."); pipeClient.ReadMode = PipeTransmissionMode.Message; } catch (NotSupportedException e) { Console.WriteLine("[CLIENT] Execption:\n {0}", e.Message); } Console.WriteLine("[CLIENT] Current TransmissionMode: {0}.", pipeClient.TransmissionMode); using (StreamReader sr = new StreamReader(pipeClient)) { // Display the read text to the console string temp; // Wait for 'sync message' from the server. do { Console.WriteLine("[CLIENT] Wait for sync..."); temp = sr.ReadLine(); } while (!temp.StartsWith("SYNC")); // Read the server data and echo to the console. while ((temp = sr.ReadLine()) != null) { Console.WriteLine("[CLIENT] Echo: " + temp); } } } } Console.Write("[CLIENT] Press Enter to continue..."); Console.ReadLine(); }}
Storm代理
2023-07-25 广告
StormProxies是一家提供动态住宅IP的服务商。动态住宅IP可以为用户提供更加灵活和稳定的网络连接,同时也可以用于一些特定的网络应用场景,例如网络游戏、视频直播等。使用StormProxies的动态住宅IP服务,用户可以通过更换IP... 点击进入详情页
本回答由Storm代理提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式