C# 使用MarshalByRefObject跨程序调用方法
来源:互联网 发布:flash动画制作软件 编辑:程序博客网 时间:2024/04/28 23:22
===================
//程序1和程序2的共有类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RemoteObject
{
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 delegate void JobAddEventHandler(CommunicationEventArg e);
public event JobAddEventHandler OnJobAdd;
public void CallJobAddEvent()
{
OnJobAdd(new CommunicationEventArg());
}
}
}
================================================
程序1(服务器端)启动时运行
参照中追加System.Runtime.Remoting
RemoteObject.CommunicationInfo _communicationInfo;
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));
//测试事件
privatevoid _communicationInfo_OnJobAdd(RemoteObject.CommunicationInfo.CommunicationEventArg e)
{
MessageBox.Show("TEST OK");
}================================================
程序2(客户端)调用方法时运行
参照中追加System.Runtime.Remoting
System.Runtime.Remoting.Channels.Tcp.TcpClientChannel _clientChannel;
_clientChannel = new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel();
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(_clientChannel, true);
RemoteObject.CommunicationInfo _communicationInfo;
_communicationInfo = (RemoteObject.CommunicationInfo)
System.Activator.GetObject(typeof(RemoteObject.CommunicationInfo),"tcp://localhost:18089/message1", System.Runtime.Remoting.WellKnownObjectMode.Singleton);
_communicationInfo.CallJobAddEvent();//启动事件(本文转自:http://hi.baidu.com/linrao/item/eef9560e5850b6103a53eef6)
- C# 使用MarshalByRefObject跨程序调用方法
- C#中MarshalByRefObject类
- MarshalByRefObject
- 使用C++ 调用 C# 程序
- 使用C++ 调用 C# 程序
- C# Shell 调用外部程序方法
- C++调用C#的DLL程序方法
- c# 一般处理程序调用js方法
- C# 调用其它程序 Process.Start 方法
- C#中Winform程序调用WebService方法
- C#对于C++程序方法的调用
- C#中Winform程序调用WebService方法
- 引用 C# MarshalByRefObject 和Serializable的区别
- C# MarshalByRefObject 和Serializable的区别
- C# 使用 Environment.GetCommandLineArgs 方法 制作*.exe和参数 供其他程序调用
- Unity-Android通信:AndroidJava 使用Unity c#编写Android程序调用任何方法
- C#实现Web程序调用Windows程序的方法
- jquery 使用ajax调用c#后台方法
- Xcode 项目后字母含义
- ADO.NET4从入门到精通/微软技术丛书(奋斗的小鸟)_PDF 电子书
- 数据库设计(Database Design)
- 远程桌面使用方法
- PDB文件:每个开发人员都必须知道的
- C# 使用MarshalByRefObject跨程序调用方法
- 有没有人知道如何从VM中取得宿主机的权限
- C#之MemberwiseClone与Clone
- 浅拷贝和深拷贝
- 数据库学习(一)
- Installation Guide for SQL Server 2008 R2
- VS 2010 SP1 Installation frozed at KB-983509
- 访问者模式
- MAT Memory Analyzer Tool 插件装配(图解)