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 Code Template
    FTP服务器文件上传的代码实现
    CentOS 7 安装vsftpd 服务器
    Nginx 403 forbidden的解决办法
    CentOS 7中使用iptables
    CentOS下安装Nginx服务器
    HTML5:web socket 和 web worker
    HTML5新增元素、标签总结
    js页面刷新之实现框架内外刷新(整体、局部)
    js页面刷新之实现定时刷新(定时器,meta)
  • 原文地址:https://www.cnblogs.com/kingteach/p/2052141.html
Copyright © 2011-2022 走看看