Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hi I am trying to make a Template, BaseResource Class, to handle the clean-up of reasources derived from my classes. I have been working with the examples I found in MSDN on Implementing the IDispose Method. The problem I'm having now is with my Constructor; How would I pass it the handle of the instance? Also can anyone tell me if I'm practicing the proper methods here? Also how could I make this thread safe?

 

Imports System
Imports System.ComponentModel

''' <summary>
''' A template class for clean up
''' </summary>
''' <remarks></remarks>
Public Class BaseResource
   '// Needed for GC(Garbage Collection)
   Implements IDisposable
#Region "GC Variables"
   ''' <summary>
   ''' Pointer to an unmanaged resource
   ''' </summary>
   ''' <remarks></remarks>
   Private shandLe As IntPtr
   ''' <summary>
   ''' Other managed resources this class uses
   ''' </summary>
   ''' <remarks></remarks>
   Private component As System.ComponentModel.Component
   ''' <summary>
   ''' Track whether Dispose has been called
   ''' </summary>
   ''' <remarks></remarks>
   Private disposed As Boolean = False
#End Region

   ''' <summary>
   ''' The class constructor
   ''' </summary>
   ''' <param name="handle">Stores the handle to this instance</param>
   ''' <remarks></remarks>
   Public Sub New(ByVal mhandlE As IntPtr)
       Me.shandLe = (mhandlE)
   End Sub

#Region "GC"
   ''' <summary>
   ''' Implement IDisposable
   ''' Do not make this method virtual
   ''' A derived class should not be able to override this method
   ''' </summary>
   ''' <param name="disposing"></param>
   ''' <remarks></remarks>
   Private Overloads Sub Dispose(ByVal disposing As Boolean)
       ' Check to see if Dispose has already been called.
       If Not (Me.disposed) Then
           ' If disposing equals true, dispose all managed 
           ' and unmanaged resources.
           If (disposing) Then
               ' Dispose managed resources.
               component.Dispose()
           End If
           ' Call the appropriate methods to clean up 
           ' unmanaged resources here.
           ' If disposing is false, 
           ' only the following code is executed.
           CloseHandle(handle)
           handle = IntPtr.Zero
       End If
       Me.disposed = True
   End Sub

   ''' <summary>
   ''' Use interop to call the methid necessary
   ''' to clean up the unmanaged resource
   ''' </summary>
   ''' <param name="handle">Pointer to unmanaged resources</param>
   ''' <returns></returns>
   ''' <remarks></remarks>
   <System.Runtime.InteropServices.DllImport("Kernel32")> _
   Private Shared Function CloseHandle(ByVal handle As IntPtr) As [boolean]
   End Function

#Region " IDisposable Support "
   ''' <summary>
   ''' This code added by Visual Basic to correctly implement the disposable pattern
   ''' </summary>
   ''' <remarks></remarks>
   Public Overloads Sub Dispose() Implements IDisposable.Dispose
       ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
       Dispose(True)
       GC.SuppressFinalize(Me)
   End Sub

   ''' <summary>
   ''' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean)
   ''' </summary>
   ''' <remarks></remarks>
   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(False)
       MyBase.Finalize()
   End Sub
#End Region
#End Region
End Class

 

Also, Would it be better to just use:

Try
   Using bResource As New BaseResource(SomeHandle)
       ' Do some stuff here
   End Using
Catch ex As Exception
  MessageBox.Show (ex.Message, ex.Source)
End Try

Edited by antecedents

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...