简体   繁体   English

使Windows窗体控件为只读和IDisposable

[英]Making Windows Forms controls read-only and IDisposable

I want to make Windows Forms controls readonly and IDisposable. 我想使Windows窗体控件为只读和IDisposable。

Is this is a good thing or a bad thing and what do I have to watch out for when calling Dispose? 这是好事还是坏事,致电Dispose时需要注意什么?

I have a Tab (PageTab) that I extend and insert a Panel which has a listview and another Toolbar control into. 我有一个选项卡(PageTab),可以将其扩展并插入具有列表视图和另一个工具栏控件的面板。 These tabs then get inserted into a tab control (all native .NET Windows Forms controls). 然后将这些选项卡插入选项卡控件(所有本机.NET Windows Forms控件)中。

When the user closes one of these tabs I call the Dispose method (which follows the MSDN way of implementing IDisposable). 当用户关闭这些选项卡之一时,我将调用Dispose方法(该方法遵循MSDN实现IDisposable的方式)。

Is it wise or suggested to declare the controls as read-only (see below)? 将控件声明为只读是明智的还是建议的(请参见下文)?

protected readonly ListView _AccountsList = new ListView();
protected readonly Panel _Panel = new Panel();

Because in the Dispose method I just call _Panel.Dipose() , etc. on them, but I cannot set them to null. 因为在Dispose方法中,我只对它们调用_Panel.Dipose()等,但是无法将它们设置为null。 I want to avoid leaks as much as I can and have things garbage collected. 我想尽可能地避免泄漏,并收集垃圾。

What's the best way for a non-Designer GUI development and disposing them? 非Designer GUI开发和处理它们的最佳方法是什么?

The default implementation of Control.Dispose (inherited by TabPage) is quite sufficient. Control.Dispose的默认实现(由TabPage继承)已经足够。 It iterates the child controls as stored in the Controls collection member and calls their Dispose() method. 迭代存储在Controls集合成员中的子控件,并调用其Dispose()方法。 You don't have to help. 你不用帮忙

There are only two cases where you should call Dispose() explicitly: 在两种情况下,应显式调用Dispose():

  • When you remove a control from the Controls collection. 从控件集合中删除控件时。 Calling Dispose() here is a hard requirement. 在这里调用Dispose()是一个硬性要求。 Not doing so will keep the window handle alive for the life of the program, it is just not visible. 否则,窗口句柄将在程序的整个生命周期内保持活动状态,只是不可见。 You'll have a leak if you don't dispose it. 如果不处理,将有泄漏。
  • When you show a form with ShowDialog(). 当您使用ShowDialog()显示表单时。 This bypasses the normal automatic disposal of a form and its child controls, necessarily so that you can read back the dialog result without risking an ObjectDisposed exception. 这将绕过正常自动处理表单及其子控件的过程,因此您可以回读对话框结果而不会冒ObjectDisposed异常的风险。 The using statement is the proper way to do this. using语句是执行此操作的正确方法。

The latter case is not in fact a leak, the Control class' finalizer ensures that the window handle for the dialog eventually is released, assuming there are no live references left to the dialog object. 后一种情况实际上并不是泄漏,Control类的终结器可以确保最终释放对话框的窗口句柄,前提是对话框对象没有剩余的活动引用。

Control is one of the very few classes where forgetting to call Dispose() can in fact cause an uncontrollable resource leak. 控件是少数几个类,其中忘记调用Dispose()实际上会导致不可控制的资源泄漏。 You are correct in that you have to call Dispose() explicitly on your TabPage derived object to dispose it when you remove the page. 您是正确的,因为您必须在TabPage派生对象上显式调用Dispose()才能在删除页面时进行处理。 But you don't have to worry about its child controls. 但是您不必担心其子控件。

If the tab is being closed, there presumably won't be any other references to it, so you don't need to worry about setting the values to null. 如果该选项卡已关闭,则大概不会有其他引用,因此您无需担心将值设置为null。

The only value in setting variables to null for the sake of GC is in cases where there would otherwise still be live references to the object. 变量设置为null的GC着想的唯一价值是在情况下,有原本仍是该对象现场引用。 It doesn't sound like that's the case here. 听起来好像不是这种情况。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM