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();
            }
        }
    }
    
  • 相关阅读:
    最短路之dijkstra算法
    拓扑排序 topsort详解
    POJ2236 Wireless Network 并查集简单应用
    最小生成树详解 prim+ kruskal代码模板
    并查集详解及模板
    POJ 3190 Stall Reservations贪心
    chapter22:源码与Tarball之(1)-传统方法编译与make方法进行编译
    vi的用法
    centos7 connect internet
    java内存区域
  • 原文地址:https://www.cnblogs.com/kingteach/p/2052141.html
Copyright © 2011-2022 走看看