⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processmessage.cs

📁 一个进程间通信列子
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Collections;

namespace AIO
{
    public enum ProcessName
    {
        PZPT = 8880,
        ZSK = 8881,
        QBJS = 8882,
        LCKZ = 8885

     
    }

    public class Process
    {
        private Hashtable _processes = Hashtable.Synchronized(new Hashtable());
        protected ProcessName _name;

        public ProcessName Name
        {
            get { return _name; }
        }

        public Process(ProcessName name)
        {
            _name = name;
            AddProcessProxy(ProcessName.PZPT);
            AddProcessProxy(ProcessName.ZSK);
            AddProcessProxy(ProcessName.QBJS);
            AddProcessProxy(ProcessName.LCKZ);
        }

        private void AddProcessProxy(ProcessName procName)
        {
            int port = (int)procName;
            _processes.Add(procName, string.Format("tcp://localhost:{0}/Proxy", port));
        }

        public virtual string OnRecieveMessage(string message, string param, ProcessName fromProcess)
        {
            return "";
        }

        public string SendMessage(string message, string param, ProcessName toProcess)
        {
            string ppxyAddress = _processes[toProcess] as string;
            if (ppxyAddress == null)
                return null;
            ProcessProxy ppxy = (ProcessProxy)Activator.GetObject(typeof(ProcessProxy), ppxyAddress);
            return ppxy.OnRecieveMessage(message, param, _name);
        }
    }

    public class ProcessProxy
        :MarshalByRefObject
    {
        private static Process _process;

        public static void InitOnServer(Process proc)
        {
            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
            Dictionary<string, string> properties = new Dictionary<string, string>();
            properties["name"] = "Client"; 
            TcpClientChannel clientChannel = new TcpClientChannel(properties,clientProvider);
            ChannelServices.RegisterChannel(clientChannel, false);
            int port = (int)(proc.Name);
            TcpServerChannel channel = new TcpServerChannel("TcpChannel",port);
     
            ChannelServices.RegisterChannel(channel, false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(ProcessProxy), "Proxy", WellKnownObjectMode.SingleCall);
            _process = proc;
        }

        public string OnRecieveMessage(string message, string param, ProcessName fromProcess)
        {
            return _process.OnRecieveMessage(message,param,fromProcess);
        }
    }    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -