今天研究了下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代码相关推荐

  1. 扫码登录原理+代码解析+完整dome

    1.引言 扫码登录这个功能,最早应该是微信的PC端开始搞,不得不说还是很神奇的. 本文将简要的介绍扫码登录功能的技术实现逻辑 一.基本技术原理 a. 扫码登录功能到底是什么样的? 首先介绍下什么是扫码 ...

  2. 腾讯地图获取当前定位的经纬度、地址的详细名称和地图上的显示

    具体步骤: 1.申请腾讯地图的开发者key  https://lbs.qq.com/console/mykey.html?console=mykey 2.引用腾讯地图的 cdn 链接 前端定位组件:h ...

  3. python-selenium登陆今日头条

    原文地址:http://www.bianbingdang.com/article_detail/148.html #python-selenium登陆今日头条 在运营今日头条的过程当中,有时候未免要进 ...

  4. python 头条_python-selenium登陆今日头条

    https://blog.csdn.net/a942242856/article/details/88379727 #python-selenium登陆今日头条 在运营今日头条的过程当中,有时候未免要 ...

  5. tradingView--K线图 使用

    tradingView--K线图 使用 相关链接 实现dome 配置 index.js black_professional.js black_simple.js dark_professional. ...

  6. Java使用 poi,pageOffice两种工具进行 word文件合并

    使用poi,pageOffice合并word操作.主要区别在于 poi可以进行后台操作,通过各种各样APi接口,直接进行文件io读写操作,,可用于后台操作,pageoffice需要前端预览后保存(根据 ...

  7. 我们一起完成插件框架的设计与实现

    我们一起完成插件框架的设计与实现 原文:我们一起完成插件框架的设计与实现 开场一些题外话,今天登陆这个"小菜"的博客园,感触颇多."小菜"是我以前在QQ群里面的 ...

  8. NLog基本讲解以及使用

    Nlog和Log4net 基本一样. 通俗讲都是记录日志的.今天先讲Nlog的使用.具体原理.....,先略. 第一步:下载 通过Nuget安装NLog,(找到项目里的"引用",右 ...

  9. 微信抢红包的简单实现

    前言 群里都在玩抢红包,抢了再发,发了再抢,简直是无聊,程序员感兴趣是的如何实现,这里简单说说实现思路,附上dome,代码有点low,好在是实现了,感兴趣的园友可以完善下,觉得还行的可以点个赞 正文 ...

最新文章

  1. Ehcache 3.0发布,修补了API并支持非堆存储
  2. linux删除非空目录
  3. 设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
  4. 详细介绍Linux硬盘挂载步骤
  5. android 6.0 api 管理,Android 6.0(API23)权限申请问题
  6. leetcode435. 无重叠区间
  7. Tidb集群加mysql_TiDB - 快速入门,集群搭建
  8. 北京严厉打击违规发布网络房源信息行为 18家机构被查处
  9. 【leveldb】整体架构
  10. 小新air15为啥没人买_联想小新air15怎么样?身边的人用的好像比较少?
  11. 【文献阅读】2004 Goktekin NS方程加弹性 A Method for Animating Viscoelastic Fluids
  12. 蒙特卡洛模型之神奇的布丰投针实验
  13. 提高项目39-电子词典
  14. CN2专线和普通国际对比优势在哪里 有什么优势
  15. 2020云服务平台领域最具商业合作价值企业盘点
  16. meta http-equiv=refresh content=0; url=是什么意思
  17. ssi 指令 php,SSI使用详解(二)_PHP教程
  18. kindeditor文本编辑器使用
  19. 2.uCOSIII任务管理
  20. java swing 跳转窗口_java swing 怎么实现点击按钮或者某个组件,跳转到另一个页面或者窗体?...

热门文章

  1. PaperNotes(19)-Learning Lane Graph Representations for Motion Forecasting
  2. 天线下倾角示意图_常用天线和无源器件技术参数汇总
  3. n个整数,其中有两个数是重复的,要求找出这两个重复的整数
  4. 光盘刻录制作Ubuntu等操作系统的启动盘
  5. codeforces 58A-C语言解题报告
  6. 并发编程(多进程1)
  7. Python基础-time and datetime
  8. case when then的用法
  9. Titanium 列表显示TableView
  10. 表单PostGet两个长度限制问题的分析