locked
What's Dispose()? RRS feed

  • Question

  • Hello

    What is Dispose exactly doing?

    And what's the difference between Me.Dispose and Me.Close? both close form.

     

    Thanks

    Thursday, May 3, 2007 4:40 PM

Answers

  • The Me.Dispose() releases the resources used by that object, calling Me.Close does not do this, so you must wait on the Garbage Collector to realease the resources for you.  The garbage collector may do this imediately or it might do it several hours later.  Me.Dispose() is the equivilant of assigning an object to Nothing in earlier versions.  However, it can affect performance and so should be used only when needed.

     

    Check out Resource Management for a little better explination

    Thursday, May 3, 2007 5:29 PM
  • The dispose is probably one of the most confusing methods. A lot of people
    want to see it as the finalize method.  In fact almost every cleanup is done by the Garbage Collector with many exceptions. (as example, window handles, streamreaders and open files).

    Moreover the disposing is done by the implementation of Idisposable that is in every designed form and designed component.

    If you close a form, it will be disposed. There is however a short while when that is not done. If you need to trap that short while there is the instruction IsDisposed, which is not in intelisense.

    http://msdn.microsoft.com/library/d...osedtopic .asp

    Be aware that one of the exceptions is a form showed with ShowDialog, where

    Dispose Method

    http://msdn.microsoft.com/library/d...osedtopic .asp

    Be aware that one of the exceptions is a form showed with ShowDialog, where

    Dispose Method

    http://msdn2.microsoft.com/en-us/library/3cc9y48w(VS.80).aspx

     

    Cleaning Up Unmanaged Resources

    http://msdn2.microsoft.com/en-us/library/498928w2(VS.80).aspx

     

     

    The garbage collection process will run periodically when it is running low on resources to recover unused memory.  Its rather a complex process under the covers but to a large extent you dont have to worry too much about ensuring you release all memory used by you application.    Simplistically: If the memory is no longer able to be referenced and the system is running low on resource, then this memory will be identified and be recovered.

     

    Garbage Collection (GC) Class

    http://msdn2.microsoft.com/en-us/library/system.gc(vs.80).aspx

     

     

     

    Friday, May 4, 2007 1:09 AM

All replies

  • I think the difference is when you do Me.Close(), you just remove the form of the screen and you still can get your form's properties.

    If you do Me.Dispose(), you destroy your form object.
    Thursday, May 3, 2007 4:57 PM
  • The Me.Dispose() releases the resources used by that object, calling Me.Close does not do this, so you must wait on the Garbage Collector to realease the resources for you.  The garbage collector may do this imediately or it might do it several hours later.  Me.Dispose() is the equivilant of assigning an object to Nothing in earlier versions.  However, it can affect performance and so should be used only when needed.

     

    Check out Resource Management for a little better explination

    Thursday, May 3, 2007 5:29 PM
  • It should also be noted that an object that has a dispose method should always have that method called when the object is no longer needed.

     

    Many classes in the framework implement the IDisposable interface. That interface has only 1 method (dispose). The reason classes implement this interface is so the class free up any unmanaged resources that may have been used by the class during its operation.


    A good example of this is a database connection. They have a dispose method because they hook into unmanaged resources when making their connection. Calling dispose on this connection object when its no longer needed frees up the unmanaged resources the class consumed.

    Thursday, May 3, 2007 8:44 PM
    Moderator
  • I noted when I use Dispose, all controls in the form return to default, but Close they don't.

    Right?

    Thursday, May 3, 2007 11:41 PM
  • Did you do dispose after close ?

     

    Thursday, May 3, 2007 11:45 PM
  •  spotty2428 wrote:

    Did you do dispose after close ?

     

    No just Dispose and it also can close the form.

    Thursday, May 3, 2007 11:53 PM
  • The dispose is probably one of the most confusing methods. A lot of people
    want to see it as the finalize method.  In fact almost every cleanup is done by the Garbage Collector with many exceptions. (as example, window handles, streamreaders and open files).

    Moreover the disposing is done by the implementation of Idisposable that is in every designed form and designed component.

    If you close a form, it will be disposed. There is however a short while when that is not done. If you need to trap that short while there is the instruction IsDisposed, which is not in intelisense.

    http://msdn.microsoft.com/library/d...osedtopic .asp

    Be aware that one of the exceptions is a form showed with ShowDialog, where

    Dispose Method

    http://msdn.microsoft.com/library/d...osedtopic .asp

    Be aware that one of the exceptions is a form showed with ShowDialog, where

    Dispose Method

    http://msdn2.microsoft.com/en-us/library/3cc9y48w(VS.80).aspx

     

    Cleaning Up Unmanaged Resources

    http://msdn2.microsoft.com/en-us/library/498928w2(VS.80).aspx

     

     

    The garbage collection process will run periodically when it is running low on resources to recover unused memory.  Its rather a complex process under the covers but to a large extent you dont have to worry too much about ensuring you release all memory used by you application.    Simplistically: If the memory is no longer able to be referenced and the system is running low on resource, then this memory will be identified and be recovered.

     

    Garbage Collection (GC) Class

    http://msdn2.microsoft.com/en-us/library/system.gc(vs.80).aspx

     

     

     

    Friday, May 4, 2007 1:09 AM
  • Thanks
    Friday, May 4, 2007 3:49 PM