.net remoting 实现通用消息处理窗口

.net remoting 实现通用消息处理窗口


  •     实现机制是制作一个cmd窗口作为信息展示窗口,主程序将需要展示的信息抛出到cmd窗口显示,以此方式做到消息的展示。

以下是cmd窗口的代码,cmd窗体作为一个消息接收的server

 1  static void Main(string[] args)
 2         {
 3             try
 4             {
 5                 //声明一个TCP通道指定端口8085
 6                 TcpChannel channel = new TcpChannel(8085);
 7                 //注册通道
 8                 ChannelServices.RegisterChannel(channel, false);
 9                 //从哪个类,发出消息
10                 RemotingConfiguration.RegisterWellKnownServiceType(typeof(ToolFunction.ConsoleMessage), "ConsoleMessage", WellKnownObjectMode.SingleCall);
11                 System.Console.WriteLine("Server:Press Enter key to exit");
12                 //等待输入
13                 System.Console.ReadLine();
14             }
15             catch (Exception)
16             {
17                 throw;
18             }
19            
20         }
  • 初始化client
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Runtime.Remoting.Channels.Tcp;
 5 using System.Runtime.Remoting.Channels;
 6 using System.Runtime.Remoting;
 7 
 8 namespace ToolFunction
 9 {
10     public class RemoteMessage
11     {
12         public static IMessage obj = null;
26 
27         /// <summary>
28         /// 初始化客户端
29         /// </summary>
30         public static void InitClient()
31         {
32             if (obj == null)
33             {
34                 TcpChannel channel = new TcpChannel();
35                 ChannelServices.RegisterChannel(channel, false);
36                 IMessage _obj = (IMessage)Activator.GetObject(typeof(IMessage), "tcp://localhost:8085/ConsoleMessage");
37                 obj = _obj;
38             }
39         }
40 
41         /// <summary>
42         /// 发送消息
43         /// </summary>
44         /// <param name="p_strMess"></param>
45         public static void SendMessage(string p_strMess)
46         {
47             try
48             {
49                 if (obj == null)
50                 {
51                     Console.WriteLine("Could not locate TCP server");
52                 }
53                 obj.ShowMess(p_strMess);
54             }
55             catch (Exception ex)
56             {
57                 CommonFunction.WriteError(ex.ToString());
58             }
59         }
60     }
61 }
  • 此类要实现一个接口MarshalByRefObject只有实现此接口才能实现remoting操作
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace ToolFunction
 6 {
 7     public interface IMessage
 8     {
 9         void ShowMess(string p_strMess);
10     }
11     public class ConsoleMessage : MarshalByRefObject, IMessage
12     {
13         #region IMessage 成员
14 
15         public void ShowMess(string p_strMess)
16         {
17             Console.WriteLine(p_strMess);
18         }
19 
20         #endregion
21     }
22 }
  • 调用示例
//打开cmd程序
string _strExePath = Application.StartupPath + @"\MessagePlatform.exe";
            Process.Start(_strExePath);
            RemoteMessage.InitClient();
  • 发送消息
RemoteMessage.SendMessage("测试消息" );
  •  最后 的样子是这样的
posted @ 2016-01-29 17:32  喜爱糖葫芦  阅读(315)  评论(0编辑  收藏  举报