In what line EXACTLY of the code above I need to put a myProcess.Dispose()
None. You don't. What you should do is create any instances of your class as part of a using
block. This will call Dispose()
at the appropriate time automatically. Failing that, always create your class as part of a try
block and call Dispose()
for it as part of the finally
block.
Is [it] better if I turn the values of those vars to a null value when I dispose the disposable objects?
No. There is no need for this. String and int variables only use memory, and the garbage collector will take care of these correctly without any extra work.
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 [do] I need to do?
You need a finalizer if your WinAPI calls allocate any system resources, such as file handles, gdi handles, threads, sockets, etc, and if this isn't as part of an existing .Net class that will handle releasing those resources for you. You only need a finalizer if both of those conditions are true. Very often, the finalizer will just call the .Dispose(False)
method, as shown in the commented example, so that your cleanup code only needs live in one place.
Therefore, when implementing IDisposable, most of the time you only need to be concerned with the first method in that sample. You may also want to uncomment the Finalize() method, but that's it. Now let's look at that method:
' 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
The trick here is that the TODO comments are confusing... misleading, even. The first comment ("dispose managed state") is completely worthless, because you can never dispose managed state on your own. This is entirely up to the garbage collector. With that in mind, you can remove that if condition entirely. The only exception I've ever found to this rule is for event handlers. You can use this place to unsubscribe any delegates in your class.
The second TODO comment ("free unmanaged resources") is more useful. It tells you where to put the clean-up code for your unmanaged resource. It just goes on too long. If it stopped after the first phrase, it would be clearer. If your class itself wraps instances of any IDisposable classes, this is a good place to call .Dipose() for objects.
The third TODO comment ("set large fields to null") is also largely unnecessary. It usually doesn't help you at all setting items to NULL in .Net. In this case, you're already disposing the object. This means it's very probably about to go out of scope anyway, and those objects would still be eligible for collection the next time the GC runs. The only reason to do this is if you suspect your object will not go out of scope soon after it's disposed. In that case, setting those fields to null may allow those larger memory blocks to be collected sooner... but this situation would be a symptom of poor design by users of your class.