Cleaning objects from form, where and when?

I have a simple application for windows forms. In the form, I have my own class, which has its own Dispose method.

So the question is when should I call it?

Is the FormClosed event (i.e. Form1_FormClosed) the right place for this? Or should I write my own Dispose method for the form?

For bonus points: Can it be reopened after closing? (Obviously, if it can, then FormClosed is the wrong way!)

Thanks.

+4
source share
4 answers

A small operation is required. Open the node next to your form in the Solution Explorer window. Double-click the Designer.cs file for the form. Find the Dispose () method and cut and paste it into the form source code file. Now you can change it and call the Dispose methods to reference one-time objects in your form class.

Pre-empting: no, it's ok to edit this part of the constructor file. Only the section in # areas is disabled.

+6
source

Controls that implement IDisposable must be added to the System.ComponentModel.IContainer components property of the form.

When disposing of the form, all disposable items in this collection will be deleted. (All pun intended)

EDIT To see this, simply release Timer on the form and look at the generated code.

+5
source

If you have a reason, then on form closing you should check bool A pplication.IsExiting .

If you want not to destroy the form, then form closing cancel the closing and do Hide() . Then the form can be reopened using Show() . The state of the whole form will remain the same.

0
source

Never call dispose unless you have a really good reason for this.

If the object contains a heavy object (image, database connection, etc.), call close as soon as you are done with it.

Calling the utility at closing will force you to reload the resource at opening. The form can be reopened if you do not destroy any important items after it is closed and save the link.

By definition, "close" does the same as "dispose" (and closes windows / forms). Each private call in the .net infrastructure simply causes a failure inside inside.

@comment

I used an application that supported 40 MB bitmaps. Call recycling on those that really helped on the memory-related machine. Calling an object to objects with the information asked in the question is impossible. Calling it โ€œjust becauseโ€ is bad, calling it because the designer makes it even worse.

-1
source

Source: https://habr.com/ru/post/1337126/


All Articles