.NET 远程处理对象生命周期
我写了这段代码:
public class Message : MarshalByRefObject, IMessage
{
...
public override object InitializeLifetimeService()
{
ILease leas = (ILease) base.InitializeLifetimeService();
if (leas != null)
{
if(leas.CurrentState == LeaseState.Initial)
{
leas.InitialLeaseTime = TimeSpan.FromMilliseconds(2000);
leas.SponsorshipTimeout = TimeSpan.Zero;
leas.RenewOnCallTime = TimeSpan.Zero;
}
}
return leas;
}
}
InitializeLifetimeService 的覆盖是否会在 2 秒后对象不再被“垃圾收集”?我的意思是,无论该实例是否被远程访问,都是独立的。
谢谢。
I wrote this code:
public class Message : MarshalByRefObject, IMessage
{
...
public override object InitializeLifetimeService()
{
ILease leas = (ILease) base.InitializeLifetimeService();
if (leas != null)
{
if(leas.CurrentState == LeaseState.Initial)
{
leas.InitialLeaseTime = TimeSpan.FromMilliseconds(2000);
leas.SponsorshipTimeout = TimeSpan.Zero;
leas.RenewOnCallTime = TimeSpan.Zero;
}
}
return leas;
}
}
Does the override of InitializeLifetimeService grantee that after 2 seconds the object is no "garbage collected"? I mean, independently if this instance was remotely accessed or not.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了确保对象租约结束,您只需将生命周期服务的轮询间隔设置为低于您设置的生命周期 2 秒。
您可以在服务器对象的构造函数中执行此操作:
现在即使您在对象上调用方法,
它仍然会被收集,因为您设置了:
To make sure the object lease ends you only need to set the life time service's poll interval to something lower than the 2 seconds you set as a life time.
you can do this in the server object`s constructor:
now even if you call mehods on the object ,
it will still be collected, because you set the: