VS generates automatically these regionized procedures when I Implement IDisposable:
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
End If
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
' Dispose(False)
' MyBase.Finalize()
'End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
Imagine that my class has one disposable object (a new Process from Process Class) that never is closed/disposed, so I want to dispose it implementing the IDisposable on the Class...
My questions are:
In what line EXACTLY of the code above I need to put a
myProcess.Dispose()
?I have some String and Integer variables which are not disposable like for example
dim myVar as string = "value"
, then is better if I turn the values of those vars to a null value when I dispose the disposable objects? something like this?:sub dispose() myProcess.Dispose() myvar = nothing end sub
My Class calls some WinAPI functions and also overrides the WndProc sub to parse messages, I need to use a finalizer or I could use the SuppressFinalize?, if I need to use the finalizer... what I need to do? just I uncomment the Finalize sub and that's all?. I'm not sure about the purpose of the Finalizer or when and how I need to use it.
While I don't know exactly the right way to implement the Dispose method, I'm disposing it in this way, but sure is totally wrong in one or other way...:
#Region " Dispose "
''' <summary>
''' Disposes all the objects created by this class.
''' </summary>
Public Sub Dispose() _
Implements IDisposable.Dispose
' Process
p.Dispose()
' Public Properties
Me.mp3val_location = Nothing
Me.CheckFileExist = Nothing
' String variables
StandardError = Nothing
StandardOutput = Nothing
Info = Nothing
Warnings = Nothing
Errors = Nothing
Tags = Nothing
' RegEx variables
Info_RegEx = Nothing
Warning_RegEx = Nothing
Fixed_RegEx = Nothing
' EventArgs Variables
StartedArgs = Nothing
ExitedArgs = Nothing
GC.SuppressFinalize(Me)
End Sub
#End Region
UPDATE
So... simplifying the confussing VS generated code to do it more intuitive and friendly about my requeriments, I should use it like this?:
Public Class Test : Implements IDisposable
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Sub Dispose(IsDisposing As Boolean)
Static IsBusy As Boolean ' To detect redundant calls.
If Not IsBusy AndAlso IsDisposing Then
' Dispose processes here...
' myProcess.Dispose()
End If
IsBusy = True
End Sub
End Class
Do not change this code.
. Move everything except theGC.SuppressFinalize
call toDispose(disposing As Boolean)
and revertDispose()
to its original form, then we can work from there. – Sam HarwellIf Not Me.disposedValue Then...
or insideIf disposing Then...
, or I should put all outside those If's? – ElektroStudios