使 Windows 窗体控件只读且 IDisposable

发布于 2024-08-04 04:15:18 字数 628 浏览 3 评论 0原文

我想将 Windows 窗体控件设为只读和 IDisposable。

这是好事还是坏事?调用 Dispose 时需要注意什么?

我有一个选项卡(PageTab),我扩展并插入一个具有列表视图和另一个工具栏控件的面板。然后,这些选项卡将插入到选项卡控件(所有本机 .NET Windows 窗体控件)中。

当用户关闭这些选项卡之一时,我调用 Dispose 方法(该方法遵循 MSDN 实现 IDisposable 的方式)。

将控件声明为只读是否明智或建议(见下文)?

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

因为在 Dispose 方法中我只是对它们调用 _Panel.Dipose() 等,但我无法将它们设置为 null。我想尽可能避免泄漏并收集垃圾。

非设计师 GUI 开发和处置的最佳方法是什么?

I want to make Windows Forms controls readonly and IDisposable.

Is this is a good thing or a bad thing and what do I have to watch out for when calling Dispose?

I have a Tab (PageTab) that I extend and insert a Panel which has a listview and another Toolbar control into. These tabs then get inserted into a tab control (all native .NET Windows Forms controls).

When the user closes one of these tabs I call the Dispose method (which follows the MSDN way of implementing 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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜柠檬 2024-08-11 04:15:18

Control.Dispose(由 TabPage 继承)的默认实现已经足够了。它迭代存储在 Controls 集合成员中的子控件并调用它们的 Dispose() 方法。你不必帮忙。

只有两种情况应显式调用 Dispose():

  • 从 Controls 集合中删除控件时。在这里调用 Dispose() 是一个硬性要求。不这样做将使窗口句柄在程序的生命周期中保持活动状态,只是不可见。如果不处理的话就会发生泄漏。
  • 当您使用 ShowDialog() 显示表单时。这会绕过窗体及其子控件的正常自动处理,这样您就可以读回对话框结果,而不会出现 ObjectDispose 异常的风险。 using 语句是执行此操作的正确方法。

后一种情况实际上并不是泄漏,Control 类的终结器确保对话框的窗口句柄最终被释放,假设没有对对话框对象留下任何实时引用。

Control 是极少数忘记调用 Dispose() 实际上会导致无法控制的资源泄漏的类之一。您是正确的,因为您必须在 TabPage 派生对象上显式调用 Dispose() 才能在删除页面时处置它。但您不必担心它的子控件。

The default implementation of Control.Dispose (inherited by TabPage) is quite sufficient. It iterates the child controls as stored in the Controls collection member and calls their Dispose() method. You don't have to help.

There are only two cases where you should call Dispose() explicitly:

  • When you remove a control from the Controls collection. Calling Dispose() here is a hard requirement. 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(). 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. The using statement is the proper way to do this.

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 is one of the very few classes where forgetting to call Dispose() can in fact cause an uncontrollable resource leak. You are correct in that you have to call Dispose() explicitly on your TabPage derived object to dispose it when you remove the page. But you don't have to worry about its child controls.

书间行客 2024-08-11 04:15:18

如果关闭该选项卡,则可能不会有任何其他对其的引用,因此您无需担心将值设置为 null。

为了 GC 而将变量设置为 null 的唯一值是在否则仍然存在对对象的实时引用的情况。听起来这里的情况并非如此。

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.

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. It doesn't sound like that's the case here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文