C# 中的远程处理和析构函数

发布于 2024-07-25 18:30:58 字数 1208 浏览 7 评论 0原文

我正在使用 .net 远程处理功能,但有一些东西我无法弄清楚,也无法在 Google 中找到答案,这就是对象处理的工作原理。

我尝试通过远程处理实现某种对象池,因为我有静态对象列表,这些对象基本上是字符串和布尔状态指示器。

当我请求一个新的远程对象时(在构造函数期间),我检查池中是否有空闲对象,将其标记为正在使用以及在对象销毁期间。 DismisObject 只是将其标记为“空闲”,

  public class MyRemotableObject : MarshalByRefObject,IDisposable
{

    private AdvancedString obj;
    public MyRemotableObject()
    {
        aso = strCache.GetFreeObject();
    }
    ~MyRemotableObject()
    {
        Destroy();
    }
    public void Dispose()
    {
        Destroy();
    }
    public void SetMessage(string message)
    {
        if (obj== null) { obj= strCache.GetFreeObject(); }
        obj.str= message;
    }
    public string GetMessage()
    {
        return obj.str;          
    }
    void Destroy()
    {
        if (obj!= null)
        {
            obj.DismisObject();
            obj = null;
        }
    }
}

超时工作正常 - 在现在活动 5 分钟后,当我尝试使用该对象时,我遇到了远程处理异常,但 ~MyRemotableObject() 不是 Dispose() 函数不会被调用,因此该对象在池中从未被标记为空闲。 即使我关闭程序 - 该对象仍然在池中保持活动状态。 释放它的唯一方法是手动调用 Dispose 函数(如果程序崩溃或用户离开打开状态,我就无法执行此操作)

有没有办法强制 .net 在关闭时处置/销毁对象连接? (我发现在某个地方,CG 应该偶尔这样做,所以我打开了 4 个客户端并崩溃了其中 2 个 - 另外 2 个客户端在一段时间后断开连接,但对象仍然标记为活动)

I am playing with the .net remoting features and there is something that I can't figure out nor find an answer in Google and is how the object disposal works.

I try to implement some kind of object pooling with remoting, for that I've list of static objects that are basicly a string and boolean state indicator.

When I request for a new remote object,(durring the consturctor) I check the pool for a free one, mark it as in-use and during the destruct of the object. the DismisObject simply marks it as "free",

  public class MyRemotableObject : MarshalByRefObject,IDisposable
{

    private AdvancedString obj;
    public MyRemotableObject()
    {
        aso = strCache.GetFreeObject();
    }
    ~MyRemotableObject()
    {
        Destroy();
    }
    public void Dispose()
    {
        Destroy();
    }
    public void SetMessage(string message)
    {
        if (obj== null) { obj= strCache.GetFreeObject(); }
        obj.str= message;
    }
    public string GetMessage()
    {
        return obj.str;          
    }
    void Destroy()
    {
        if (obj!= null)
        {
            obj.DismisObject();
            obj = null;
        }
    }
}

The timeouts work fine - after 5 minutes of now activity when I try to use the object I got remoting exception but ~MyRemotableObject() not the Dispose() functions aren't called so the object is never marked as free in the pool. Even if I close the program - still the object remains active in the pool. The only way to free it is manually call the Dispose function (which I can't do if, for example the program crashes or the user leaves is open )

Is there a way to force .net to dispose/destruct the objects when it closes the connection?
(I found in some place that the CG should do that once in a while, so I opened 4 clients and crashed 2 them - the other 2 got disconnected after a while but the objcets are still marked as active)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕巷 2024-08-01 18:30:59

您可以使用 ITrackingHandler 来跟踪对象何时断开连接,然后在此时运行 Dispose 代码。

You could possibly use ITrackingHandler to track when your object is disconnected and then run the Dispose code at that point.

此岸叶落 2024-08-01 18:30:59

尽可能使用在 using 构造中实现 IDisposable 的对象。

例如,

using (var remotableObj = new MyRemotableObject())
{
    // use it
}

更多信息此处以及此处

Use objects that implement IDisposable in a using construct whenever possible.

e.g.

using (var remotableObj = new MyRemotableObject())
{
    // use it
}

More info here and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文