码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2015-08-26 15:38:13      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

先看一下代码

辅助类

技术分享
  1  public class ProcessMessage
  2     {
  3         /// <summary>
  4         /// 主程序常量
  5         /// </summary>
  6         private const string STRCLIENTNAME = "AutoRunClient";
  7 
  8         /// <summary>
  9         /// 消息委托
 10         /// </summary>
 11         /// <param name="strmsg">消息</param>
 12         public delegate void DelegateMsg(object objmsg,object objfrom);
 13         /// <summary>
 14         /// 消息事件
 15         /// </summary>
 16         public event DelegateMsg OnMsg;
 17 
 18         /// <summary>
 19         /// 功能描述:发送消息到主程序
 20         /// 作  者:huangzh
 21         /// 创建日期:2015-08-26 12:20:17
 22         /// 任务编号:
 23         /// </summary>
 24         /// <param name="objMsg">objMsg</param>
 25         public void SendMsgToMainClient(object objMsg)
 26         {
 27             try
 28             {
 29                 SendMsgToProcess(STRCLIENTNAME, objMsg);
 30             }
 31             catch
 32             {
 33                 throw;
 34             }
 35         }
 36 
 37         /// <summary>
 38         /// 功能描述:读取主程序消息
 39         /// 作  者:huangzh
 40         /// 创建日期:2015-08-26 12:20:34
 41         /// 任务编号:
 42         /// </summary>
 43         /// <returns>返回值</returns>
 44         public void ReadMsgFromMainClient()
 45         {
 46             try
 47             {
 48                 ReadMsgFromProcess(STRCLIENTNAME);
 49             }
 50             catch
 51             {
 52                 throw;
 53             }
 54         }
 55 
 56         /// <summary>
 57         /// 功能描述:发送消息到指定进程
 58         /// 作  者:huangzh
 59         /// 创建日期:2015-08-26 12:20:50
 60         /// 任务编号:
 61         /// </summary>
 62         /// <param name="strToolProcessName">进程名</param>
 63         /// <param name="objMsg">objMsg</param>
 64         public void SendMsgToProcess(string strToolProcessName, object objMsg)
 65         {
 66             try
 67             {
 68                 IMailBox mail = new ProcessMailBox(strToolProcessName, 1024, false);
 69                 mail.Content = objMsg;
 70                 Thread.Sleep(10);
 71                 mail.ClearContent();
 72             }
 73             catch
 74             {
 75                 throw;
 76             }
 77         }
 78 
 79         /// <summary>
 80         /// 功能描述:从指定进程读取消息
 81         /// 作  者:huangzh
 82         /// 创建日期:2015-08-26 12:21:11
 83         /// 任务编号:
 84         /// </summary>
 85         /// <param name="strToolProcessName">进程名</param>
 86         /// <returns>返回值</returns>
 87         public void ReadMsgFromProcess(string strToolProcessName)
 88         {
 89             try
 90             {
 91                 if (OnMsg == null)
 92                 {
 93                     throw new Exception("请初始化OnMsg事件");
 94                 }
 95                 Thread th = new Thread(delegate()
 96                 {
 97                     IMailBox mail = new ProcessMailBox(strToolProcessName, 1024, false);
 98                     while (true)
 99                     {
100                         if (OnMsg != null)
101                         {
102                             OnMsg(mail.Content, strToolProcessName);
103                             Thread.Sleep(10);
104                         }                        
105                     }
106                 });
107                 th.IsBackground = true;
108                 th.Start();
109 
110             }
111             catch
112             {
113                 throw;
114             }
115         }
116     }
View Code

调用

技术分享
 1  [STAThread]
 2         static void Main(string[] args)
 3         {
 4             //Test test = new Test();
 5             //if(args.Length > 0)
 6             //    test.RunWriter();
 7             //else
 8             //    test.RunReader();
 9             if (args.Length > 0)
10             {
11                 //writer
12                 ProcessMessage message = new ProcessMessage();
13                 while (true)
14                 {
15                     Console.WriteLine("输入内容");
16                     string strmsg = Console.ReadLine();
17                     for (int i = 0; i < 100; i++)
18                     {
19                         message.SendMsgToMainClient(i + " " + strmsg);
20                         message.SendMsgToProcess("Test", i + " " + strmsg);
21                     }
22                 }
23             }
24             else
25             {
26                 //reader
27                 ProcessMessage message = new ProcessMessage();
28                 message.OnMsg += new ProcessMessage.DelegateMsg(message_OnMsg);
29                 message.ReadMsgFromMainClient();
30                 message.ReadMsgFromProcess("Test");
31                 while (true)
32                 {
33                     System.Threading.Thread.Sleep(1000);
34                 }
35             }
36         }
37 
38         static void message_OnMsg(object objmsg, object objfrom)
39         {
40             Console.WriteLine("from:[" + objfrom + "]:" + objmsg);
41         }
View Code

再看下效果图

技术分享

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

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

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

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

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

标签:

原文地址:http://www.cnblogs.com/bfyx/p/4760419.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!