zoukankan      html  css  js  c++  java
  • C# 命名管道进程间通信

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO.Pipes;
    using System.Security.Principal;
    
    namespace TopInfo.Metevation.Common
    {
        public class PipeMessageUtil
        {
            private const string PipeName = "TopInfoPipe";
    
            public static bool NeedSendMessage = false;
    
            private static NamedPipeServerStream pipeServer;
    
            private static NamedPipeClientStream pipeClient;
    
            public static void OpenPiepServer()
            {
                if (pipeServer == null)
                {
                    pipeServer = new NamedPipeServerStream(PipeName,
                                        PipeDirection.Out, 1,
                                        PipeTransmissionMode.Byte,
                                        PipeOptions.Asynchronous);
    
                    pipeServer.BeginWaitForConnection(
                        delegate
                        {
                            NeedSendMessage = true;
                            pipeServer.WaitForConnection();
                            //pipeServer.EndWaitForConnection();
                        },
                        null);
                }
            }
    
            public static void ClosePipeServer()
            {
                if (pipeServer != null && pipeServer.IsConnected)
                    pipeServer.Close();
                NeedSendMessage = false;
            }
    
            public static void SendMessage(string msg)
            {
                if (NeedSendMessage && pipeServer.IsConnected && pipeServer.CanWrite)
                {
                    StreamString ss = new StreamString(pipeServer);
                    ss.WriteString(msg);
                }
            }
    
            public delegate void PrintMessage(string msg);//委托接受消息处理方法
    
            public static void ReciverMessage(PrintMessage printMessage)
            {
                if (pipeClient == null)
                {
                    pipeClient = new NamedPipeClientStream(".", PipeName, PipeDirection.In, PipeOptions.None, TokenImpersonationLevel.None);
                    pipeClient.Connect();
                }
                while (pipeClient.CanRead)
                {
                    StreamString ss = new StreamString(pipeClient);
                    printMessage(ss.ReadString());
                }
                pipeClient.Close();
            }
        }
    }
    
  • 相关阅读:
    Java二维数组
    Java实现冒泡排序
    python类简记
    LaTeX:论文Instruction最后一段的章节引用
    在Adobe IIIustator中加入数学公式——配合MathType使用
    org.springframework.web.bind.annotation不存在
    xshell5 下载安装
    MultipartFile解析Excel
    开源项目环境搭建
    学习路线
  • 原文地址:https://www.cnblogs.com/kingteach/p/2052141.html
Copyright © 2011-2022 走看看