IDisposable を実装すると、VS はこれらのリージョン化されたプロシージャを自動的に生成します。
#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
クラスに 1 つの使い捨てオブジェクト (プロセス クラスからの新しいプロセス) があり、それが決して閉じられたり破棄されたりしないと想像してください。そのため、クラスに IDisposable を実装して破棄したいと考えています...
私の質問は次のとおりです。
上記のコードのどの行に ?を入力する必要があり
myProcess.Dispose()
ますか?たとえば、使い捨てではないString変数とInteger変数がいくつか
dim myVar as string = "value"
あります。使い捨てオブジェクトを破棄するときに、これらの変数の値を null 値にするとよいでしょうか? このようなもの?:sub dispose() myProcess.Dispose() myvar = nothing end sub
私のクラスはいくつかの WinAPI 関数を呼び出し、メッセージを解析するために WndProc サブルーチンもオーバーライドします。ファイナライザーを使用する必要があるか、SuppressFinalize を使用できますか? Finalizeサブのコメントを外すだけで、それだけですか?. Finalizer の目的や、いつ、どのように使用する必要があるのかわかりません。
Dispose メソッドを実装する正しい方法が正確にはわかりませんが、この方法で破棄していますが、いずれかの方法で完全に間違っていることは確かです...:
#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
アップデート
だから...混乱するVS生成コードを単純化して、私の要求についてより直感的で使いやすいようにするには、次のように使用する必要がありますか?:
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