在VB.NET表单的设计器文件中配置方法

时间:2011-09-12 15:19:07

标签: vb.net winforms visual-studio-2008 .net-3.5

<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try

有人可以告诉我为什么我们在designer.vb中使用它?

1 个答案:

答案 0 :(得分:3)

Dispose用于释放未由运行时管理的资源(非托管资源)。这包括文件,流,字体等。garbage collector在不再使用该对象时自动释放分配给托管对象的内存。但是,无法预测垃圾收集何时发生。此外,垃圾收集器不了解非托管资源,例如窗口句柄,或打开文件和流。


在您的代码中,基类的Dispose方法被子类实现覆盖,因此overriden关键字调用Mybase.Dispose。基类为IContainer,子类为FormDispose界面中提供了designer.vb方法。


Dispose中,此自动生成的代码用于在表单上调用Dispose时调用表单组件上的<System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) Try ' If Dispose() has been called explicitly free both managed and ' unmanaged resources (disposing = true) ' and there are components in the form If disposing AndAlso components IsNot Nothing Then ' Free the resources used by components components.Dispose() End If Finally ' Once done disposing the current form's resources, ' dispose the resources held by its base class ' Finally clause executes this code block (to dispose base class) ' whether or not an exception has been encountered while ' disposing the resources in the form MyBase.Dispose(disposing) End Try ,即表单处理时,也要处理它的组件。

{{1}}

更多信息