' Ref: http://msdn2.microsoft.com/en-us/library/system.idisposable.aspx Imports System.Threading Imports System.Threading.Thread Module Module1 Sub Main() Dim resource3 As New resourceType("3") resource3.DoSomething() resource3 = Nothing DoNext() Using resource1 As New resourceType("1") resource1.DoSomething() End Using DoNext() ' THE FOLLOWING TRY CONSTRUCTION IS EQUIVALENT TO THE USING BLOCK Dim resource2 As New resourceType("2") Try resource2.DoSomething() Catch ex As Exception Console.WriteLine("Error: " & ex.Message) Finally ' ' Insert code to do additional processing before disposing of resource. resource2.Dispose() End Try DoNext Console.ReadKey() End Sub Private Sub DoNext() Console.WriteLine() GC.Collect() Thread.Sleep(3000) Console.WriteLine() End Sub End Module Public Class resourceType Implements IDisposable Private m_ID As String Private disposed As Boolean = False Public Sub New(ByVal strID As String) m_ID = strID Console.WriteLine("Instantiated: " & m_ID) End Sub Public Sub DoSomething() Console.WriteLine("Look at me! [" & m_ID & "]") End Sub ' Implement IDisposable. Public Overloads Sub Dispose() Implements IDisposable.Dispose GC.SuppressFinalize(Me) Console.WriteLine("disposed [" & m_ID & "]") End Sub Protected Overrides Sub Finalize() ' Do not re-create Dispose clean-up code here. ' Calling Dispose(false) is optimal in terms of ' readability and maintainability. Dispose() Console.WriteLine("Finalized [" & m_ID & "]") End Sub End Class