【问题标题】:Dispose method in VB.NET form's designer fileVB.NET 表单设计器文件中的 Dispose 方法
【发布时间】:2011-11-15 10:49:48
【问题描述】:
<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 中使用它?

【问题讨论】:

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


    【解决方案1】:

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


    在您的代码中,基类的Dispose 方法被子类实现覆盖,因此overriden 关键字调用Mybase.Dispose。基类是IContainer,子类是FormDispose 方法在IDisposable 接口中可用。


    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
    

    更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 2014-01-24
      • 1970-01-01
      相关资源
      最近更新 更多