在什么情况下脱离事件必要? [英] In what cases are detaching from events necessary?

查看:121
本文介绍了在什么情况下脱离事件必要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我是否完全清楚附着在对象中的事件的含义。



这是我目前的理解,正确或精心设计: / p>

1。



示例:



this.Closing + = new System.ComponentModel.CancelEventHandler(MainWindow_Closing);



public event EventHandler OnMyCustomEvent = delegate {};



我假设当您的对象被处理或垃圾回收时,这些函数被取消分配,并将自动从事件。



2。附加到不再需要的对象(= null;)必须从分离



示例:
附加到计时器的Elapsed事件,你只回应一次。我会假设您需要将Timer存储在本地变量中,这样您可以在事件触发后分离Elapsed事件。因此,在本地方法范围内声明定时器会导致泄漏:



System.Timers.Timer myDataTimer = new System.Timers .Timer(1000);
myDataTimer.Elapsed + = new System.Timers.ElapsedEventHandler(myDataTimer_Elapsed);



3。附加到您课堂的本地对象中的事件不需要处理?



例如,如果您有一个ObservableCollection,您创建,监视和让死。如果您使用本地私有函数附加到CollectionChanged事件,则当您的类被垃圾回收时,这个函数将不会释放,导致ObservableCollection也被释放?



我确定我有一些地方停止使用对象,并且没有脱离事件(例如,我制作的定时器示例),所以我正在寻找一个更清楚的解释,这是如何工作的。

解决方案

我认为你使它比它需要更复杂。您只需要记住两件事:




  • 当您订阅一个活动时,活动的所有者(发布商)通常会保留参考给你订阅的代表。

  • 如果你使用一个实例方法作为委托的动作,那么委托对它的target对象有一个引用。



这意味着如果你写:

 发布者。 SomeEvent + = subscriber.SomeMethod; 

然后订阅者将不符合

请注意,在许多情况下,订阅者只是这个

 发布者.SomeEvent + = myDataTimer_Elapsed; 

相当于:

  publisher.SomeEvent + = this.myDataTimer_Elapsed; 

假设它是一个实例方法。



反向关系仅仅是因为事件订阅 - 换句话说,用户不会让发布者活着。



请参阅我关于事件和代表的文章,以获取更多信息。


I'm not sure if I'm entirely clear on the implications of attaching to events in objects.

This is my current understanding, correct or elaborate:

1. Attaching to local class events do not need to be detached

Examples:

this.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);

public event EventHandler OnMyCustomEvent = delegate { };

I'm assuming that when your object is disposed or garbage collected, the functions are deallocated and would automatically detach from the events.

2. Attaching to objects you no longer need (= null;) have to be detached from

Examples: Attaching to a timer's Elapsed event, which you only respond to once. I would assume you need to store the Timer in a local variable so you can detached the Elapsed event after the event fires. Thus, declaring the timer in a local method scope like so would result in a leak:

System.Timers.Timer myDataTimer = new System.Timers.Timer(1000); myDataTimer.Elapsed += new System.Timers.ElapsedEventHandler(myDataTimer_Elapsed);

3. Attaching to events in a local object to your class does not require disposing?

For example, if you had an ObservableCollection that your creates, monitors, and lets die. If you attached to the CollectionChanged event using a local, private function, wouldn't this function deallocate when your class is garbage collected, causing the ObservableCollection to also be freed?

I'm sure I have places where I've stopped using objects and have failed to detach from an event (for example, the timer example I made), so I'm looking for a clearer explanation on how this works.

解决方案

I think you're making it more complicated than it needs to be. You just need to remember two things:

  • When you subscribe to an event, the event's "owner" (the publisher) generally keeps a reference to the delegate you subscribe with.
  • If you use an instance method as the action of a delegate, then the delegate has a reference to its "target" object.

This means that if you write:

publisher.SomeEvent += subscriber.SomeMethod;

Then subscriber won't be eligible for garbage collection before publisher is unless you unsubscribe later.

Note that in many cases, subscriber is just this:

publisher.SomeEvent += myDataTimer_Elapsed;

is equivalent to:

publisher.SomeEvent += this.myDataTimer_Elapsed;

assuming it's an instance method.

There is no reverse relationship just due to event subscription - in other words the subscriber doesn't keep the publisher alive.

See my article on events and delegates for more information, by the way.

这篇关于在什么情况下脱离事件必要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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