使用IDisposable的退订事件 [英] Using IDisposable to unsubscribe events

查看:352
本文介绍了使用IDisposable的退订事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理从一个WinForms控制事件的类。根据用户正在做什么,我deferencing类的一个实例,并创建一个新的处理同样的事件。我需要先退订该事件的旧实例 - 很容易。我想如果可能的话,为此在一个非专有的方式,而且好像这是IDisposable的工作。然而,大多数文档推荐的IDisposable使用非托管资源,只有当,它在这里并不适用。

I have a class that handles events from a WinForms control. Based on what the user is doing, I am deferencing one instance of the class and creating a new one to handle the same event. I need to unsubscribe the old instance from the event first - easy enough. I'd like to do this in a non-proprietary manner if possible, and it seems like this is a job for IDisposable. However, most documentation recommends IDisposable only when using unmanaged resources, which does not apply here.

如果我实现IDisposable,并从该事件中的Dispose(退订),我会委曲其用意何在?我应该不是提供一个退订()函数调用?

If I implement IDisposable and unsubscribe from the event in Dispose(), am I perverting its intention? Should I instead provide an Unsubscribe() function and call that?


编辑:下面是一些虚拟的code样的,显示我在做什么(使用IDisposable接口)。我的实际实施相关的一些专有的数据绑定(长的故事)。

Here's some dummy code that kind of shows what I'm doing (using IDisposable). My actual implementation is related to some proprietary data binding (long story).

class EventListener : IDisposable
{
    private TextBox m_textBox;

    public EventListener(TextBox textBox)
    {
        m_textBox = textBox;
        textBox.TextChanged += new EventHandler(textBox_TextChanged);
    }

    void textBox_TextChanged(object sender, EventArgs e)
    {
        // do something
    }

    public void Dispose()
    {
        m_textBox.TextChanged -= new EventHandler(textBox_TextChanged);
    }
}

class MyClass
{
    EventListener m_eventListener = null;
    TextBox m_textBox = new TextBox();

    void SetEventListener()
    {
        if (m_eventListener != null) m_eventListener.Dispose();
        m_eventListener = new EventListener(m_textBox);
    }
}

在实际的code,在事件监听器类更多地参与,并且每个实例是唯一有显著。我在一个集合里用它们,创建/销毁它们作为用户点击左右。

In the actual code, the "EventListener" class is more involved, and each instance is uniquely significant. I use these in a collection, and create/destroy them as the user clicks around.


结论

我接受<一href="http://stackoverflow.com/questions/452281/using-idisposable-to-unsubscribe-events#452321">gbjbaanb's回答,至少现在是这样。我觉得用一个熟悉的界面远远超过使用它的,其中不涉及非托管code任何可能下跌的好处(怎么会这个对象的用户甚至不知道吗?)。

I'm accepting gbjbaanb's answer, at least for now. I feel that the benefit of using a familiar interface outweighs any possible downside of using it where no unmanaged code is involved (how would a user of this object even know that?).

如果有人不同意 - 请发布/评论/编辑。如果一个更好的理由可以对IDisposable的进行,那么我会改变接受的答案。

If anyone disagrees - please post/comment/edit. If a better argument can be made against IDisposable, then I'll change the accepted answer.

推荐答案

是的,去了。虽然有人认为IDisposable的只有非托管资源实现的,这种情况并非如此 - 非托管资源恰好是最大的胜利,并实现它最明显的原因。我认为它收购了这个想法,因为人们想不出用任何其他原因。它不喜欢finaliser这是一个性能问题,并且不容易的GC处理好。

Yes, go for it. Although some people think IDisposable is implemented only for unmanaged resources, this is not the case - unmanaged resources just happens to be the biggest win, and most obvious reason to implement it. I think its acquired this idea because people couldn't think of any other reason to use it. Its not like a finaliser which is a performance problem and not easy for the GC to handle well.

把任何整齐,高达code。在您的处置方法。这将是更清晰,更清洁,显著更可能prevent内存泄漏和一个该死的视线更容易正确地使用比试图记住联合国做你的参考。

Put any tidy-up code in your dispose method. It'll be clearer, cleaner and significantly more likely to prevent memory leaks and a damn sight easier to use correctly than trying to remember to un-do your references.

IDisposable接口的目的是让你的code更好地工作,您无需做大量的体力劳动。使用它的力量在你的青睐,并得到了一些人为的设计意图废话。

The intention of IDisposable is to make your code work better without you having to do lots of manual work. Use its power in your favour and get over some artificial "design intention" nonsense.

我记得是很难足以说服确定性结束的用处微软早在.NET刚出来 - 我们赢得了战斗,并说服他们加入它(即使它只是在当时是一个设计模式),使用它!

I remember it was difficult enough to persuade Microsoft of the usefulness of deterministic finalisation back when .NET first came out - we won the battle and persuaded them to add it (even if it was only a design pattern at the time), use it!

这篇关于使用IDisposable的退订事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆