CookieMonster24 Posted September 22, 2003 Posted September 22, 2003 if i'm closing a form and use the method myForm.close, should i call myForm.dispose? i read that the close method disposes the resources, so i guess i what i really want to do is confirm that this is correct. so...if i'm using the command myForm.showdialog, then i want to call myForm.dispose once the form has been closed closed. but if i'm just using myForm.show, then all i need to close my form is call myForm.close. is this correct? Quote
*Experts* mutant Posted September 22, 2003 *Experts* Posted September 22, 2003 THe Close method calls the Dispose() method for you so you dont have to worry about it. Quote
AlexCode Posted September 22, 2003 Posted September 22, 2003 mutant is right and in general you can use close freely... The only diference that was told to me while taking the MCSD was that the Dispose will force the Garbage Collector to throw away the object automaticly... :D So, if you really need this use it... if not... let the garbage man deal with it when "he" have time! :p Quote Software bugs are impossible to detect by anybody except the end user.
paulx3 Posted September 24, 2003 Posted September 24, 2003 if you use form.dispose take not that any code in the Form.Close or Closing events will not be executed Quote
*Experts* Nerseus Posted September 24, 2003 *Experts* Posted September 24, 2003 Holy cow there's some bad info in this thread! Close method calls the Dispose() method for you so you dont have to worry about it That's only true for non-modal windows. For Modal windows, you should call Dispose. If you never care about getting a result from the modal window, you can call Dispose in the form's Closed event. Normally you will call Dispose in the calling form after you've gotten back any values from the modal window that you need. Dispose will force the Garbage Collector to throw away the object automaticly Not quite right. If your object holds onto any resources (window handles and such), you should call Dispose to have them released immediately. That means objects you create that hold onto resources should release them that way (or through an explicit method like Close). The GC will not collect it immediately though - it will still get collected whenever the GC runs and determines that there are no more references to the object. paulx3 is right - You should never call Dispose on a form to close it - you should always use the Close method and Dispose if needed. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
samsmithnz Posted September 24, 2003 Posted September 24, 2003 you should always use the Close method and Dispose if needed. -Nerseus Thanks for your input Nerseus, but I'm still a little unclear on this. When DO I need the dispose method? And if the garbage collector always cleans up after me, why I should I do some of its job? Quote Thanks Sam http://www.samsmith.co.nz
*Gurus* divil Posted September 24, 2003 *Gurus* Posted September 24, 2003 The garbage collector only knows about .NET resources, not unmanaged resources. Forms and graphics use unmanaged resource extensively so it's good practice to always call .Dispose on _any_ object that has it, when you're done with it. Because of the great programmers at Microsoft, your system won't become unstable if you don't free up resources (if you're using a recent operating system). You'll just be a bad programmer. :) Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted September 24, 2003 Posted September 24, 2003 Bad programmer!! I'd better make some changes!!! This brings to light a few new things. Quote C#
AlexCode Posted September 24, 2003 Posted September 24, 2003 Nerseus - Sorry if a said something "less true" but in this kind of issue we can't pruve when the GC really collects the damn trash! :) I've just explained it as I was told on the MCSD! :D Thanks! Quote Software bugs are impossible to detect by anybody except the end user.
*Experts* Nerseus Posted September 24, 2003 *Experts* Posted September 24, 2003 No big deal, Alex :) As divil said, better safe than sorry for the most part. It won't hurt to call Dispose when you're done with a Form, or any object for that matter. When MUST you call Dispose? Never, really - but you might get exceptions later on. If you always call Dispose on a Form, you're probably Ok. If you happen to create any Graphics objects through a call to CreateGraphics, then you must call Dispose on that Graphics object. It mentions that in the help. For other objects, you'll have to look at the help to see if you MUST (or *should*) call Dispose. For the most part, the answer is No since most objects won't do anything with any system resources. If you have an object that opens a file, for instance, then you'd want to Close that file yourself rather than wait for the GC to do it (that may happen minutes or hours later). Normally that's done through a method named Close, but you could put that close logic in your Dispose method and remember to call Dispose. -Ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.