SHARE
TWEET

Class collector: dispose every class added with one line

ShadowTzu Feb 13th, 2015 234 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Coded by ShadowTzu
  2. 'Free to use
  3.  
  4. 'my 3D engine, Tzu3D: http://shadowtzu.free.fr
  5. 'Youtube: https://www.youtube.com/user/shadowtzu
  6. 'Facebook: https://www.facebook.com/Tzu3d
  7. 'Twitter: https://twitter.com/shadowtzu
  8. 'Twitch: http://www.twitch.tv/shadowtzu
  9. 'Website: http://tzu3d.weebly.com
  10.  
  11. ''' <summary>
  12. ''' Collect class for dispose every class added with one line
  13. ''' one call to ClassCollector.Instance.Dispose and every class added are disposed
  14. ''' </summary>
  15. ''' <remarks></remarks>
  16. Friend Class ClassCollector
  17.     Implements IDisposable
  18.  
  19. #Region "Singleton"
  20.     Private Shared _ClassCollector As ClassCollector
  21.     Private Shared ReadOnly mylock As New Object()
  22.  
  23.     Public Shared Function Instance() As ClassCollector
  24.         SyncLock (mylock)
  25.             If _ClassCollector Is Nothing Then
  26.                 _ClassCollector = New ClassCollector
  27.             End If
  28.             Return _ClassCollector
  29.         End SyncLock
  30.     End Function
  31.  
  32.     Public Shared Function IsInstanced() As Boolean
  33.         If _ClassCollector Is Nothing Then Return False
  34.         Return True
  35.     End Function
  36. #End Region
  37.  
  38.     Private mClassList As Collection
  39.  
  40.     Private Sub New()
  41.         mClassList = New Collection
  42.     End Sub
  43.  
  44.     Friend Sub Add(ByRef AnyClass As Object)
  45.         mClassList.Add(AnyClass)
  46.     End Sub
  47.  
  48. #Region "Destructor: Dispose Every class added"
  49.     Private disposedValue As Boolean = False
  50.     Protected Friend Overridable Sub Dispose(ByVal disposing As Boolean)
  51.         If Not Me.disposedValue Then
  52.             If disposing Then
  53.  
  54.                 For Each anyclass As IDisposable In mClassList
  55.                     anyclass.Dispose()
  56.                     anyclass = Nothing
  57.                 Next
  58.  
  59.                 mClassList.Clear()
  60.                 mClassList = Nothing
  61.             End If
  62.  
  63.  
  64.         End If
  65.         Me.disposedValue = True
  66.     End Sub
  67.  
  68.     Friend Sub Dispose() Implements IDisposable.Dispose
  69.         Dispose(True)
  70.         GC.SuppressFinalize(Me)
  71.     End Sub
  72. #End Region
  73.  
  74. End Class
RAW Paste Data
Top