正在部署相当于处理其子加上自己的小组? [英] Is disposing a panel equivalent to disposing its children plus itself?

查看:98
本文介绍了正在部署相当于处理其子加上自己的小组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的类:

class MyPanel : Panel
{
    ...
    protected override void Dispose(bool disposing)
    {
        // My code here
    }
}

是以下两个code样品相同呢?

are the following two code samples equivalent?

base.Dispose(disposing);

VS

if (disposing)
{
    List<Control> ctrls = new List<Control>(this.Controls);
    this.Controls.Clear();  
    foreach(Control c in ctrls)
    {
         c.Dispose();
    }
}
base.Dispose(disposing);

如果他们有不同的效果,那会是什么?

If they have a different effect, what would it be?

编辑:请问这是因为,无论出于何种原因,这样做的第一种方式冻结我的程序前,先要到处置任何其子(的处理是真实的,而控件包含2个控制器),而第二路工作正常。如果我能愉快地使用第二个,那么这是伟大的。

I ask this because, for whatever reason, doing it the first way freezes my program before it gets to disposing any of its children (disposing is true, and Controls contains 2 controls), whereas the second way works fine. If I can happily use the second one, then that's great.

推荐答案

这两个是不相同的,因为你可以很容易地通过查看 Control.Dispose 的实施看使用反射(容易做到的,同时它还是免费的!)

The two are not identical, as you can easily see by looking at the implementation of Control.Dispose using Reflector (easy to do while it's still free!)

例如,如果你只是调用Dispose在面板上,它会调用每个子控件DisposeAxControls,并从其母公司自身删除,其子项上调用Dispose之前。

For example, if you just call Dispose on your panel, it will call DisposeAxControls on each child control, and remove itself from its parent, before calling Dispose on its children.

不过我同意科迪灰色 - 你需要找出为什么它冻结,而不是试图用你的建议解决方法,以扫在地毯下的问题

Nevertheless I agree with Cody Gray - you need to work out why it's freezing rather than trying to "sweep the problem under the carpet" with your proposed workaround.

通常你不调用Dispose一个小组明确 - 如果你这样做,你需要确保你正确地管理您的控件的生命周期和所有权

Normally you don't call Dispose on a Panel explicitly - if you are doing so, you need to be sure you're managing the lifetime and ownership of your controls correctly.

要做的事情就是开始逐步简化您的应用程序步骤,直到它不再冻结 - 工作是什么原因造成的冻结(例如,通过删除你提到一个接一个的计时器 - 这听起来犯罪嫌疑人)。一旦你有一个很简单的例子,展示这个问题它会更容易为你(或有人在这里)制定出了什么事情。

The thing to do is to start simplifying your app step by step until it no longer freezes - to work out what is causing the freeze (for example, by removing the Timers you mentioned one by one - that sounds suspect). Once you have a very simple example that exhibits the problem it will be easier for you (or someone here) to work out what's going on.

这篇关于正在部署相当于处理其子加上自己的小组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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