if(object==null)
{
.....
}

解决方案 »

  1.   

    原文COPY:)The way to do it is to derive a "tracking handler" object from
    ITrackingHandler:// interface that enables clients to be notified when the client proxy
    // has disconnected.
    public interface IDisconnectHandler
    {
        void OnClientDisconnected();
    }// this class receives notification of .NET Remoting events.
    public class RemotingObjectTrackingHandler : ITrackingHandler
    {
        public RemotingObjectTrackingHandler()
        {
            // register this object to with Remoting subsystem
            TrackingServices.RegisterTrackingHandler(this);
        }   ~RemotingObjectTrackingHandler()
        {
            TrackingServices.UnregisterTrackingHandler(this);
        }    public void MarshaledObject(object obj, ObjRef or)
        {
        }    public void UnmarshaledObject(object obj, ObjRef or)
        {
        }    public void DisconnectedObject(object obj)
        {
            // we only want to notify objects that care
            IDisconnectHandler i = obj as IDisconnectHandler;
            if( i != null )
            {
                i.OnClientDisconnected();
            }
        }
    }Now derive your class, MyRemoteObject, from IDisconnectHandler, and
    implement the OnClientDisconnected interface method:public class MyRemoteObject : MarshalByRefObject, IDisconnectHandler
    {
        // ...    public void OnClientDisconnected()
        {
            // remoting proxy disconnected - unregister your object
            Unregister();
        }    // ...
    }One important thing to note is that Remoting won't discover that the proxy
    object has been closed until the LeaseTime expires.  Therefore, if you need
    to be notified about disconnected clients sooner, rather than later, you
    need to configure the lease time for the object to some low value.For example, add these lines to your Main function:static void Main()
    {
        // call before you register/create your objects
        // ideally leases should be configured on a per-object basis by
    overriding the
        // InitializeLifetimeService() method.
        LifetimeServices.LeaseTime = TimeSpan.FromSeconds(10);    // ...
    }Your objects will now be notified approximately 10 seconds after a client
    disconnects.Hope this helps you.Regards,Joel Dudgeon
    Consultant/Programmer
    Unisys Canada Inc.