日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MarshalByRefObject 的DOME代码

發布時間:2023/12/19 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。