进程间通信,就是这么简单

2019-09-11 15:32:31 浏览数 (1)

先看一下代码

辅助类

代码语言:javascript复制
 public class ProcessMessage
    {
        /// <summary>
        /// 主程序常量
        /// </summary>
        public static string MainProcessID = "Main";
        public static string OtherProcessID = "Test";

        /// <summary>
        /// 消息委托
        /// </summary>
        /// <param name="strmsg">消息</param>
        public delegate void DelegateMsg(object objmsg, object objfrom);
        /// <summary>
        /// 消息事件
        /// </summary>
        public event DelegateMsg OnMsg;   

       
        /// <summary>
        /// 功能描述:发送消息到指定进程
        /// 作  者:huangzh
        /// 创建日期:2015-08-26 12:20:50
        /// 任务编号:
        /// </summary>
        /// <param name="strToolProcessName">进程名</param>
        /// <param name="objMsg">objMsg</param>
        public void SendMsgToProcess(string strToolProcessName, object objMsg)
        {
            try
            {
                ProcessMailBox mail = new ProcessMailBox(strToolProcessName, 1024, true);
                mail.Content = objMsg;
                mail.Dispose();
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 功能描述:从指定进程读取消息
        /// 作  者:huangzh
        /// 创建日期:2015-08-26 12:21:11
        /// 任务编号:
        /// </summary>
        /// <param name="strToolProcessName">进程名</param>
        /// <returns>返回值</returns>
        public void ReadMsgFromProcess(string strToolProcessName)
        {
            try
            {
                if (OnMsg == null)
                {
                    throw new Exception("请初始化OnMsg事件");
                }
                Thread th = new Thread(delegate()
                {
                    IMailBox mail = new ProcessMailBox(strToolProcessName, 1024, false);
                    while (true)
                    {
                        if (OnMsg != null)
                        {
                            OnMsg(mail.Content, strToolProcessName);
                            mail.ClearContent();
                        }
                    }
                });
                th.IsBackground = true;
                th.Start();

            }
            catch
            {
                throw;
            }
        }
    }

调用

代码语言:javascript复制
class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                //writer
                ProcessMessage message = new ProcessMessage();
                while (true)
                {
                    Console.WriteLine("输入内容");
                    string strmsg = Console.ReadLine();
                    message.SendMsgToProcess(ProcessMessage.MainProcessID, strmsg);
                    message.SendMsgToProcess(ProcessMessage.OtherProcessID, strmsg);
                }
            }
            else
            {
                //reader
                ProcessMessage message = new ProcessMessage();
                message.OnMsg  = new ProcessMessage.DelegateMsg(message_OnMsg);
                message.ReadMsgFromProcess(ProcessMessage.MainProcessID);
                message.ReadMsgFromProcess(ProcessMessage.OtherProcessID);
                Console.ReadKey();
            }
        }

        static void message_OnMsg(object objmsg, object objfrom)
        {
            Console.WriteLine("from:["   objfrom   "]:"   objmsg);
        }
    }

再看下效果图

怎么样是不是心动了呢?别慌,往下看

首先辅助类需要一个dll,ThreadMessaging.dll 猛戳我下载这个DLL

然后你就可以华丽丽的使用了,话说这个DLL竟然有源码,如果需要可以留下你的某些联系方式哦

当然,如果你还有其他什么好的建议也可以留下的。

0 人点赞