且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

事件处理程序停止工作后处置

更新时间:2021-12-07 22:11:32

只有在一切都完全用它做处置的对象。试图用它做配置会导致不确定的行为后,对象什么。把它看成是释放内存。

Only Dispose an object after everything is completely done with it. Trying to do anything with the object after it is disposed will result in undefined behavior. Think of it as freeing the memory.

幸运的是,当你关闭Windows表单的形式部署这些对象为您服务。您需要处置他们在您的code的唯一情况是如果从表单中删除的对象。

Fortunately, Windows forms disposes these objects for you when you close the form. The only case you would need to dispose them in your code is if you remove the objects from the form.

看看在了.Designer.cs文件,你会看到窗体的Dispose()方法:

Take a look at the .Designer.cs file, and you will see the form's Dispose() method:

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

该components.Dispose()将递归清理表上的所有部件,包括那些在面板上。

The components.Dispose() will recursively clean up all the components on the form, including those in the panel.