Windows Develop Bookmark and Share   
 index > Windows Forms General > I don't understand Control.Dispose()
 

I don't understand Control.Dispose()

Hi, this is actually a little embarassing but I've discovered that I don't really understand what happens when calling the Control.Dispose() method. I have read the documentation stating that it releases unmanaged resources and optionally managed resources. I always thought that meant it removes it all from memory and destroys all references to the object but apparently at least the references part is wrong.

My confusion in action: I have a parent user control that contains 2 more user controls.The parent user control passes messages between its two contained user controls. User control 1 uses a timer to periodically refresh a list. User control 2 has a button that does 2 things: 1.It fires an event recieved by the parent control which tells its form todecide if the entire parent control should be closed. 2. Tells usercontrol 1 to start its timer and update its list (in the case that it was not closed by the form). Please accept this process as a premise; theres a lot more to it than I am explaining. The problem is that if the form chooses to close the parent control, I call dispose on that control. My thinking was that all child controls would be recursively disposed and dereferenced and their processing would be aborted. In action, the child components are disposed but then step 2 above continues processing after disposal and the timer continues to run. That means that I am then trying to refresh the disposed list. My goal is to just get rid of the parent control efficiently when the parent form decides its no longer needed.

Questions: Can someone explain what happens when you call the Dispose(true) method? Can someone tell me the best way to get rid of this control when the event is fired? Thanks.

xr280xr  Monday, November 26, 2007 11:27 PM

Hello.

To understand what's going on when the Dispose is called, take a look at:

http://msdn.microsoft.com/msdnmag/issues/07/07/CLRInsideOut/default.aspxfor an overview and to:

http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539aefor an detailed expanation onIDisposable, finalizers and memory management.

Calling Dispose() on an object doesn't remove themanaged object from the memory. The managed object is removed only by the garbage collector, and onlywhenthere are no references left to it.

If you need to remove imediatelly amanaged object (I doubt you would really need this in your case),you need to make sure that there are no references left to it and after thatyou need to call GC.Collect() to force a garbage collection

After you have called Dispose on the parent user-control, have you tried to remove the control from the form's list of control, by doing yourForm.Control.Remove(yourParentControl) ?

Hope this helps...

bb35  Tuesday, November 27, 2007 5:38 AM

Hello.

To understand what's going on when the Dispose is called, take a look at:

http://msdn.microsoft.com/msdnmag/issues/07/07/CLRInsideOut/default.aspxfor an overview and to:

http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539aefor an detailed expanation onIDisposable, finalizers and memory management.

Calling Dispose() on an object doesn't remove themanaged object from the memory. The managed object is removed only by the garbage collector, and onlywhenthere are no references left to it.

If you need to remove imediatelly amanaged object (I doubt you would really need this in your case),you need to make sure that there are no references left to it and after thatyou need to call GC.Collect() to force a garbage collection

After you have called Dispose on the parent user-control, have you tried to remove the control from the form's list of control, by doing yourForm.Control.Remove(yourParentControl) ?

Hope this helps...

bb35  Tuesday, November 27, 2007 5:38 AM
Good articles, thank you. I don't need to remove the object immediately but my goal was to have the garbage collection grab it as soon as possible. I call dispose and then de-reference the object (...Remove(myParentControl)). But I guess the problem is that since that control is running its own timer thread, it continues to run on its own. I think I need to check if the control is disposing at the time the list tries to refresh to avoid the problem.
xr280xr  Tuesday, November 27, 2007 4:20 PM

Hello

Just a quick note: a timer doesn't involve any thread. Timers work using windows messages. Whenever the time-out value for the timer elapses, the system posts a WM_TIMER message to the window associated with the timer.When the applicationgets the WM_TIMER from the message queue, it runs the Timer.Tick event handler.
bb35  Tuesday, November 27, 2007 5:21 PM

Thanks for the correction. I read that somewhere in another forum that did not have a correction. A quick note on your note: a System.Windows.Forms.Timer doesn't involve a seperate thread.

xr280xr  Tuesday, November 27, 2007 5:27 PM

Yes, that is correct. It's the System.Windows.Forms.Timer who doesn't involve a separate thread.

System.Threading.Timer will execute the TimerCallback on a separate thread, so that one will involve a separate thread Smile

bb35  Tuesday, November 27, 2007 5:43 PM

You can use google to search for other answers

Custom Search

More Threads

• Creating custom extended Calendar control
• CBT Hook not working in Windows Vista
• Clear Datagrid past records
• How to call a form like asp.net pages
• IE .net toolbar (C# Zolnikov's Toolband and .net kbbar) on IE 7: toolbar unmovable!
• Using USB Scanner with Winforms app
• Button next to scrollbar in treeview
• (.NET2) owner drawn treeview itemwidth
• How to log the events to textfile?
• Data Duplication Check