home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_DefiningCleanup_vb________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2001-08-21  |  2.3 KB  |  73 lines

  1. Option Explicit On
  2. Option Strict On
  3.  
  4. Imports System
  5. Imports System.IO
  6.  
  7.  
  8. ' If the base class does not implement IDisposable then
  9. ' you should implement the IDisposable interface
  10. ' if you want to be cleaned up with C#'s "using" statement
  11. ' Public Dispose must also implement IDisposable.Dispose
  12. Class MyType : Implements IDisposable
  13.  
  14.    ' Called when not explicitly Disposed
  15.    Protected Overrides Sub Finalize()
  16.       Console.WriteLine("Executing Finalize()")
  17.       Dispose(false)
  18.    End Sub
  19.  
  20.    ' Called when we want to clean up all objects
  21.    ' Should also implement IDisposable.Dispose
  22.    Public Sub Dispose() Implements IDisposable.Dispose
  23.       Console.WriteLine("Executing Dispose()")
  24.       ' Essentially, this sets a flag.
  25.       ' If set, then GC will not call Finalize (and thus not Dispose(false))
  26.       ' If NOT set, then GC will free the state by calling Finalize(false)
  27.       GC.SuppressFinalize(Me)
  28.       Dispose(true)
  29.    End Sub 'Dispose
  30.  
  31.    ' 1. If flagged, dispose of managed objects
  32.    ' 2. Always cleans up *un*managed objects
  33.    ' 3. When necessary, clean up base objects with MyBase.Dispose
  34.    Protected Sub Dispose(disposing as Boolean)
  35.       Console.WriteLine("Executing Dispose(" & disposing.ToString() & ")")
  36.       If disposing Then ' "true" when we have managed objects to clean up
  37.           ' Dispose of our own managed objects
  38.            Console.WriteLine("Dispose: This is where we cleanup managed objects")
  39.       End If 
  40.  
  41.       ' Dispose of our own *un*managed object
  42.       Console.WriteLine("Dispose: This is where we cleanup *un*managed objects")
  43.  
  44.       ' When appropriate, dispose of base object
  45.       'MyBase.Dispose(disposing)
  46.    End Sub 'Dispose
  47.  
  48.    ' Typically used for objects that may be re-opened 
  49.    Public Sub Close()
  50.       Console.WriteLine("Executing Close()")
  51.       Me.Dispose()
  52.    End Sub
  53.  
  54.    Public Sub DoSomething()
  55.       Console.WriteLine("MyType object is doing something...")
  56.    End Sub
  57. End Class
  58.  
  59.  
  60. Class App
  61.    Shared Sub Main()
  62.       Console.WriteLine("Executing App.Main()")
  63.       Dim mt As New MyType()
  64.       mt.DoSOmething()
  65.  
  66.       ' If the following line is not executed, then the
  67.       ' GC will call Finalize() at some point in the future
  68.       mt.Dispose()
  69.  
  70.       Console.Write("Press Enter to close window...")
  71.       Console.Read()
  72.    End Sub
  73. End Class