简体   繁体   English

在关闭wpf中的窗口时,我是否必须取消订阅事件?

[英]do i have to unsubscribe from events when closing a window in wpf?

the tiltle says it all, in my window (not the main window) constructor i have 在我的窗口(不是主窗口)构造函数中,倾斜说明了一切

EventAggregator.OnUserLoggedIn += OnUserLoggedIn;
EventAggregator.OnUserLoggedOff += OnUserLoggedOff;

is there a difference between 两者之间有区别吗?

this.Close()

and

EventAggregator.OnUserLoggedIn -= OnUserLoggedIn;
EventAggregator.OnUserLoggedOff -= OnUserLoggedOff;
this.Close()

i've read that closing the window disposes all unmanged resources, are those events considered maneged or unmanaged? 我已经读到关闭窗口会处理所有未管理的资源,这些事件被认为是被管理的还是不受管理的?

Those events are managed resources if anything (I'm not sure if you can call events resources though). 这些事件是托管资源,如果有的话(我不确定你是否可以调用事件资源)。

Yes, you should unhook event handlers from child windows (not main window as it doesn't matter) otherwise you may experience memory leaks as garbage collector won't be able to pick up those objects as still having references. 是的,您应该从子窗口取消事件处理程序(不是主窗口,因为它无关紧要)否则您可能会遇到内存泄漏,因为垃圾收集器将无法获取这些对象仍然具有引用。

Check this blog for more info on memory leaks and event handlers: 有关内存泄漏和事件处理程序的更多信息,请查看此博客

A classic leak common to all .NET applications, and a common oversight by developers. 所有.NET应用程序常见的经典泄漏,以及开发人员的共同监督。 If you create an event handler to handle events occurring in some other object then, if you don't clear the link when you've finished, an unwanted strong reference will be left behind. 如果您创建一个事件处理程序来处理某些其他对象中发生的事件,那么,如果您在完成后没有清除该链接,则会留下不需要的强引用。

If you are closing the main window and your application will terminate after that you don't have to worry about unhooking the handler . 如果您正在closing the main window并且您的应用程序将在此之后终止,您don't have to worry about unhooking the handler All memory associated with your process is available for reclaim once process terminates. 一旦进程终止,与进程关联的所有内存都可用于回收。

But in case you are talking about secondary window here and your process will continue to run , you should consider unhooking the events since window will always be in memory as long as EventAggregator stays in memory and resulting in memory leak in your window class. 但是如果你talking about secondary window这里talking about secondary window并且你的进程将继续运行,你应该consider unhooking the events因为只要EventAggregator停留在内存中并且导致窗口类中的内存泄漏,窗口将始终在内存中。 Publisher (EventAggregator) will hold on to the subscriber object (window) as long as it stays in memory . Publisher (EventAggregator) will hold on to the subscriber object (window) as long as it stays in memory So, window object will persist with the lifetime of EventAggregator object. 因此,window对象将持续使用EventAggregator对象的生命周期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在所有情况下,我是否必须取消订阅所有活动? - Do I HAVE TO unsubscribe from all events in all cases? 我什么时候退订自定义控件中的事件 - When do I unsubscribe from events inside Custom Control 在c#中使用后,我是否必须取消订阅按钮事件? - Do I have to unsubscribe from button events after using it in c#? 我需要为我的 BackgroundWorker 处理和取消订阅事件吗 - Do i need to dispose and unsubscribe from events for my BackgroundWorker 当发布者和处理者是同一个对象时,我应该取消订阅事件吗? - Should I unsubscribe from events when publisher and handler are the same object? 打开新窗口时,如何防止WPF应用关闭主窗口? - How can I prevent WPF app from closing main window when opening a new window? C# WPF:当 window 关闭时,如何调用具有数据绑定的方法? - C# WPF: How do I call a method with data binding when a window is closing? 关闭 wpf window 时如何判断 await 是否正在运行或已完成运行 - How do I tell if await is running or has finished running when closing wpf window 我是否需要取消订阅局部变量的匿名事件处理程序? - Do i have to unsubscribe from anonymous event handlers of local variables? 我是否需要取消订阅表格中的活动? - Do I need to unsubscribe events in my Form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM