Control.Dispose メソッド
定義
サーバー コントロールが、メモリから解放される前に最終的なクリーンアップを実行できるようにします。Enables a server control to perform final clean up before it is released from memory.
public:
virtual void Dispose();
public virtual void Dispose ();
abstract member Dispose : unit -> unit
override this.Dispose : unit -> unit
Public Overridable Sub Dispose ()
実装
例
次のコード例では、メソッドをオーバーライドして、 Dispose HtmlTextWriter コントロールに関連付けられているオブジェクトを閉じ、と Dispose いう名前のコントロールに対してメソッドを呼び出し Button myButton
ます。The following code example overrides the Dispose method to close the HtmlTextWriter object associated with a control, and call the Dispose method on a Button control, named myButton
. Exceptionこのバージョンのメソッドが呼び出されたときにがスローされた場合 Dispose 、コントロールは現在のオブジェクトにメッセージを書き込み HttpResponse ます。If an Exception is thrown when this version of the Dispose method is called, the control writes a message to the current HttpResponse object.
public override void Dispose()
{
try
{
Context.Response.Write("Disposing " + ToString());
// Perform resource cleanup.
myTextWriter.Close();
myButton.Dispose();
}
catch(Exception myException)
{
Context.Response.Write("Exception occurred: "+myException.Message);
}
}
Public Overrides Sub Dispose()
Try
Context.Response.Write("Disposing " & ToString())
' Perform resource cleanup.
myTextWriter.Close()
myButton.Dispose()
Catch myException As Exception
Context.Response.Write("Exception occurred: " & myException.Message)
End Try
End Sub
注釈
Dispose を使い終わったら Control を呼び出します。Call Dispose when you are finished using the Control. Dispose メソッドによって、Control は使用不可の状態になります。The Dispose method leaves the Control in an unusable state. このメソッドを呼び出した後、使用していたメモリがガベージコレクションによって解放されるように、コントロールへのすべての参照を解放する必要があります。After calling this method, you must release all references to the control so the memory it was occupying can be reclaimed by garbage collection.