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"));//启动事件 
           

        }

 

posted @ 2012-08-26 01:23  双柳居士  阅读(354)  评论(0编辑  收藏  举报