江西医疗网站备案前置审批,每月网站流量,河北移动端网站建设,软件工程主要课程今天研究了下MarshalByRefObject跨程序通讯#xff0c;由于今天很晚了#xff0c;先贴出DOME代码。 分别建立2个winform程序#xff0c;WinClient和WinServer#xff0c;2个项目中都有CommunicationInfo类#xff08;你也可以将CommunicationInfo做成一个类库供2个winform… 今天研究了下MarshalByRefObject跨程序通讯由于今天很晚了先贴出DOME代码。 分别建立2个winform程序WinClient和WinServer2个项目中都有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