Re: Object destruction VB.NET 2005



Hi Scott,
I tried implementing IDisposable in my class and when I close the
application the Dispose method is not being called. If I break using the
debugger the blocked thread is still blocked. Since the object running that
thread is a member of the object I need to dispose will "Dispose" ever get
called before the thread exits?
Sid.

"Scott M." <smar@xxxxxxxxxxxxx> wrote in message
news:ejcDoLpbIHA.5400@xxxxxxxxxxxxxxxxxxxxxxx
You should implement the IDisposable interface with a proper
Dispose/Finalize pattern. As a rule, you don't actually put your clean up
code in the Finalize method, you put it into a Dispose method. When the
user of the class instantiates it, they can/should use a "Using" block, so
that the object will call Dispose automatically when the end of the Using
block is hit. Or the class user could manually call Dispose when they are
done using it. And, if all else fails, the Finalze method will call
Dispose if/when the object is collected.

http://msdn2.microsoft.com/en-us/library/b1yfkh5e(vs.71).aspx

-Scott


"Sid Price" <sid@xxxxxxxxxxx> wrote in message
news:uwMVsEpbIHA.4936@xxxxxxxxxxxxxxxxxxxxxxx
Hi everyone,
I have a class that creates a thread for some I/O that blocks in the
absence of any activity. I need to be able to shut down that thread when
the application using the class closes. I tried putting my code to close
the thread in the Finalize call of the object but when the app closes
Finalize is not called. A simple solution is to create a public "Close"
method for the object however I would have hoped for an automatic method
since the class in question is to be a reusable class library and it
seems better practice for it to clean up when the instances are destroyed
without code needing to be called by the application using the library.
Any suggestions please?
Sid.





.



Relevant Pages

  • Re: Automatic Dispose
    ... But, a SqlConnection itselft is a managed resource and, as such, should not ... instance user uses "Using" and Dispose is called automatically). ... Public Overridable Sub Dispose() Implements IDisposable.Dispose ... Protected Overrides Sub Finalize() ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Automatic Dispose
    ... instance user uses "Using" and Dispose is called automatically). ... Public Overridable Sub Dispose() Implements IDisposable.Dispose ... Protected Overrides Sub Finalize() ... Private con As SqlClient.SqlConnection ' managed resource this class ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Dispose must be thread-safe ?
    ... If Dispose was called from program -> the object was reachable at the start ... thread will be blocked by simple reentry protection if ... Finalize, but this simple reentry protection guaranteed to work in all cases ... > You wrote type B and you wrote your Finalize method without any regard ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Lowdown on finalization
    ... Because the method no longer referes to o, ... Finalize is a method of object. ... Dispose - release resources. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Weird Dispose/Finalize behavior
    ... If you're not using unmanaged resources directly, ... Dispose() would call SuppressFinalizeso your code works ... GC.ReRegisterForFinalize(this); // Enables Finalize ... Managed resouces are not being freed by the garbage collector. ...
    (microsoft.public.dotnet.framework.compactframework)