共用方式為


How to: Implement the Dispose Finalize Pattern (Visual Basic) 

The Dispose Finalize pattern ensures that resources are released when the object is no longer needed.

Example

The ResourceClass class in the following example utilizes managed and unmanaged resources and then uses the Dispose Finalize pattern to dispose of them properly. The resources and their functions are:

  • The implementation of the Dispose method, which allows users of the class to dispose of class instances. This method calls Dispose(True) to dispose of the object's resources, and then calls SuppressFinalize to prevent the finalization code from executing a second time.

  • The override of the base Finalize method, which allows the common language runtime (CLR) garbage collector to dispose of class instances. This method calls Dispose(False) to dispose of the object's resources. Notice that if Dispose was called previously for the object, its call to SuppressFinalize would prevent the garbage collector from calling the Finalize method.

  • The overload of the Dispose method, which does the disposing work. It takes a Boolean parameter, disposing, which indicates whether your code initiated the object's disposal. When you dispose of an object, all of its resources must be disposed of. When the CLR garbage collector disposes of an object, only the unmanaged resources must be disposed of; the garbage collector automatically disposes of the managed resources when necessary.

For more information, see Object Lifetime: How Objects Are Created and Destroyed.

    Public Class ResourceClass
        Implements IDisposable

        Private managedResource As System.ComponentModel.Component
        Private unmanagedResource As IntPtr
        Protected disposed As Boolean = False

        Public Sub New()
            ' Insert appropriate constructor code here.
        End Sub

        Protected Overridable Overloads Sub Dispose( _
            ByVal disposing As Boolean)
            If Not Me.disposed Then
                If disposing Then
                    managedResource.Dispose()
                End If
                ' Add code here to release the unmanaged resource.
                unmanagedResource = IntPtr.Zero
                ' Note that this is not thread safe.
            End If
            Me.disposed = True
        End Sub

        Public Sub AnyOtherMethods()
            If Me.disposed Then
                Throw New ObjectDisposedException(Me.GetType().ToString, _
                    "This object has been disposed.")
            End If
        End Sub

#Region " IDisposable Support "
        ' Do not change or add Overridable to these methods.
        ' Put cleanup code in Dispose(ByVal disposing As Boolean).
        Public Overloads Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
        Protected Overrides Sub Finalize()
            Dispose(False)
            MyBase.Finalize()
        End Sub
#End Region
    End Class

This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Visual Basic Language. For more information, see How to: Insert Snippets Into Your Code (Visual Basic).

Compiling the Code

This example requires:

These changes must be made in the code:

  • Replace ResourceClass with the name of the class that implements IDisposable.

  • Use the test in AnyOtherMethods in any methods using resources that may have been disposed of.

  • Replace the managedResource declaration with declarations of any managed objects in your class that need to be disposed of. If a class implements IDisposable or has a Close method, it probably needs to be disposed of. In the Dispose method, close or dispose of these objects.

  • Replace the unManagedResource declaration with declarations of unmanaged objects in your class that need to be disposed of. The method for disposing of these objects depends on how the object is defined. For details, consult the documentation on the object.

Robust Programming

Once the Dispose method has been called, contained objects in your collection will not be valid. You should test the disposed field before performing any operations on your object. For an example, see the AnyOtherMethods method in the code example.

See Also

Reference

IDisposable

Concepts

Implementing a Dispose Method

Other Resources

Garbage Collection