2011-09-12 60 views
1
<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窗體的設計器文件中配置方法

回答

3

Dispose用於釋放未由運行時管理的資源(非託管資源)。這包括文件,流,字體等。當不再使用該對象時,garbage collector會自動釋放分配給管理對象的內存。但是,無法預測垃圾收集何時發生。此外,垃圾收集器不知道非託管資源,如窗口句柄或打開的文件和流。


在代碼中,基類的Dispose方法由子類實現覆蓋,因此overriden關鍵字調用Mybase.Dispose。基類是​​,子類是FormDispose方法在IDisposable接口中可用。


designer.vb,這自動生成的碼用來呼籲形式的組分DisposeDispose時被調用的形式被佈置在所述形式時即上,處置其組件。

<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 

更多信息

相關問題