Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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

Software bugs are impossible to detect by anybody except the end user.
  • *Experts*
Posted

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

"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
  • *Gurus*
Posted

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. :)

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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!

Software bugs are impossible to detect by anybody except the end user.
  • *Experts*
Posted

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

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...