MarshalByRefObject 的DOME代码
生活随笔
收集整理的這篇文章主要介紹了
MarshalByRefObject 的DOME代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??????? 今天研究了下MarshalByRefObject跨程序通訊,由于今天很晚了,先貼出DOME代碼。
?
分別建立2個winform程序,WinClient和WinServer,2個項目中都有CommunicationInfo類(你也可以將CommunicationInfo做成一個類庫供2個winform程序引用)。
?
貼上代碼:
CommunicationInfo.cs
CommunicationInfo .cs using System; using System.Collections.Generic; using System.Linq; using System.Text;public class CommunicationInfo : MarshalByRefObject{public CommunicationInfo(){}public override object InitializeLifetimeService(){//return base.InitializeLifetimeService(); System.Runtime.Remoting.Lifetime.ILease aLease= (System.Runtime.Remoting.Lifetime.ILease)base.InitializeLifetimeService();if (aLease.CurrentState == System.Runtime.Remoting.Lifetime.LeaseState.Initial){// 不過期aLease.InitialLeaseTime = TimeSpan.Zero;}return aLease;}public class CommunicationEventArg : EventArgs{public string Name = string.Empty;}public delegate void JobAddEventHandler(CommunicationEventArg e);public event JobAddEventHandler OnJobAdd;public void CallJobAddEvent(string name){CommunicationEventArg ee = new CommunicationEventArg();ee.Name = name;OnJobAdd(ee);}}WinServer
View Code //測試事件private void _communicationInfo_OnJobAdd(RemoteObject.CommunicationInfo.CommunicationEventArg e){textBox1.Text = textBox1.Text + "\r\n" + e.Name;}RemoteObject.CommunicationInfo _communicationInfo = null;private void button1_Click(object sender, EventArgs e){if (_communicationInfo == null){System.Runtime.Remoting.Channels.Tcp.TcpServerChannel servChannel = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(18089);System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(servChannel, true);_communicationInfo = new RemoteObject.CommunicationInfo();_communicationInfo.OnJobAdd += new RemoteObject.CommunicationInfo.JobAddEventHandler(_communicationInfo_OnJobAdd);System.Runtime.Remoting.RemotingServices.Marshal(_communicationInfo, "message1",typeof(RemoteObject.CommunicationInfo));}}WinClient
View Code RemoteObject.CommunicationInfo _communicationInfo;private void button1_Click(object sender, EventArgs e){if (_communicationInfo == null){System.Runtime.Remoting.Channels.Tcp.TcpClientChannel _clientChannel;_clientChannel = new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel();System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(_clientChannel, true);_communicationInfo = (RemoteObject.CommunicationInfo)System.Activator.GetObject(typeof(RemoteObject.CommunicationInfo), "tcp://127.0.0.1:18089/message1", System.Runtime.Remoting.WellKnownObjectMode.Singleton);}_communicationInfo.CallJobAddEvent(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));//啟動事件 }?
轉載于:https://www.cnblogs.com/shunhe316/archive/2012/08/26/2657082.html
總結
以上是生活随笔為你收集整理的MarshalByRefObject 的DOME代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell 自动输入密码
- 下一篇: JQuery. Parse XML ch