• <bdo id='Xb11S'></bdo><ul id='Xb11S'></ul>
  • <legend id='Xb11S'><style id='Xb11S'><dir id='Xb11S'><q id='Xb11S'></q></dir></style></legend>

      <i id='Xb11S'><tr id='Xb11S'><dt id='Xb11S'><q id='Xb11S'><span id='Xb11S'><b id='Xb11S'><form id='Xb11S'><ins id='Xb11S'></ins><ul id='Xb11S'></ul><sub id='Xb11S'></sub></form><legend id='Xb11S'></legend><bdo id='Xb11S'><pre id='Xb11S'><center id='Xb11S'></center></pre></bdo></b><th id='Xb11S'></th></span></q></dt></tr></i><div id='Xb11S'><tfoot id='Xb11S'></tfoot><dl id='Xb11S'><fieldset id='Xb11S'></fieldset></dl></div>
      1. <tfoot id='Xb11S'></tfoot>

        <small id='Xb11S'></small><noframes id='Xb11S'>

      2. 在什么情况下脱离事件是必要的?

        In what cases are detaching from events necessary?(在什么情况下脱离事件是必要的?)

              <tbody id='l4vEw'></tbody>
          • <legend id='l4vEw'><style id='l4vEw'><dir id='l4vEw'><q id='l4vEw'></q></dir></style></legend>

          • <small id='l4vEw'></small><noframes id='l4vEw'>

              <tfoot id='l4vEw'></tfoot>
                <bdo id='l4vEw'></bdo><ul id='l4vEw'></ul>
              • <i id='l4vEw'><tr id='l4vEw'><dt id='l4vEw'><q id='l4vEw'><span id='l4vEw'><b id='l4vEw'><form id='l4vEw'><ins id='l4vEw'></ins><ul id='l4vEw'></ul><sub id='l4vEw'></sub></form><legend id='l4vEw'></legend><bdo id='l4vEw'><pre id='l4vEw'><center id='l4vEw'></center></pre></bdo></b><th id='l4vEw'></th></span></q></dt></tr></i><div id='l4vEw'><tfoot id='l4vEw'></tfoot><dl id='l4vEw'><fieldset id='l4vEw'></fieldset></dl></div>

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

                  问题描述

                  我不确定我是否完全清楚附加到对象中的事件的含义.

                  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.附加到本地类事件不需要分离

                  例子:

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

                  公共事件 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.附加到您不再需要的对象 (= null;) 必须从中分离

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

                  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.将本地对象中的事件附加到您的类不需要处置?

                  例如,如果您有一个 ObservableCollection,您可以创建、监控并让它消失.如果您使用本地私有函数附加到 CollectionChanged 事件,当您的类被垃圾回收时,该函数不会释放,导致 ObservableCollection 也被释放吗?

                  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:

                  • 当您订阅某个事件时,该事件的所有者"(发布者)通常会保留对您订阅的委托的引用.
                  • 如果您使用实例方法作为委托的操作,则委托具有对其目标"对象的引用.

                  这意味着如果你写:

                  publisher.SomeEvent += subscriber.SomeMethod;
                  

                  那么在 publisher 之前,subscriber 将没有资格进行垃圾回收,除非您稍后取消订阅.

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

                  请注意,在许多情况下,subscriber 只是 this:

                  Note that in many cases, subscriber is just this:

                  publisher.SomeEvent += myDataTimer_Elapsed;
                  

                  相当于:

                  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.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C# namespace alias - what#39;s the point?(C# 命名空间别名 - 有什么意义?)
                  Using Xpath With Default Namespace in C#(在 C# 中使用具有默认命名空间的 Xpath)
                  IBM.Data.DB2.Core connection problems(IBM.Data.DB2.Core 连接问题)
                  Generating an EDMX from a DB2 Database(从 DB2 数据库生成 EDMX)
                  Datetime field overflow with IBM Data Server Client v9.7fp5(IBM Data Server Client v9.7fp5 的日期时间字段溢出)
                  Using entity Framework with .NET Core and DB2(将实体框架与 .NET Core 和 DB2 结合使用)

                      <tfoot id='i3rYu'></tfoot>
                        <bdo id='i3rYu'></bdo><ul id='i3rYu'></ul>
                          <tbody id='i3rYu'></tbody>
                      • <small id='i3rYu'></small><noframes id='i3rYu'>

                          1. <i id='i3rYu'><tr id='i3rYu'><dt id='i3rYu'><q id='i3rYu'><span id='i3rYu'><b id='i3rYu'><form id='i3rYu'><ins id='i3rYu'></ins><ul id='i3rYu'></ul><sub id='i3rYu'></sub></form><legend id='i3rYu'></legend><bdo id='i3rYu'><pre id='i3rYu'><center id='i3rYu'></center></pre></bdo></b><th id='i3rYu'></th></span></q></dt></tr></i><div id='i3rYu'><tfoot id='i3rYu'></tfoot><dl id='i3rYu'><fieldset id='i3rYu'></fieldset></dl></div>
                          2. <legend id='i3rYu'><style id='i3rYu'><dir id='i3rYu'><q id='i3rYu'></q></dir></style></legend>