What is the right way to get rid of a new form without closing it immediately?

So, in my applications, I try to create new instances of forms on the fly, and then use Form.Show () to display them (not modally).

private void test_click(object sender, EventArgs e) { var form = new myForm(); form.Show(); } 

However, Code Cracker tells me that these forms should be deleted. So, I wrapped them in a "using" statement, but then they close immediately after opening.

 using (var form = new myForm()) { form.Show(); } 

I do not want to use Form.ShowDialog (), because in several cases I open new windows that show only reports; I do not need them to be fashionable.

+6
c # winforms dispose
Sep 14 '16 at 21:57
source share
4 answers

Hmm, "code cracker" seems to be a very suitable term for this tool, its advice certainly made you write code that breaks your program. The golden rule is to never trust IDisposable tips from a static code analysis tool, none of them have ever had enough understanding in code execution. They can never tell which call to Dispose () is doing the job.

What he does not see is that the Form class already knows how to handle itself. It is very easy for this, the object becomes unusable when the window closes. When the window is no longer there is no reason to continue to use the Form object. A luxury that is not very common in .NET, but is certainly inspired by the very smart programmers who worked on Xerox 45 years ago.

There is only one special rule that you should keep in mind; it is not utilized when you use ShowDialog () to display a window. It was deliberate, it makes getting the results of the dialogue too risky. Using the using statement to call ShowDialog () is very easy to do, the call does not return until the window is closed.

+6
Sep 14 '16 at 22:28
source share

Do I need to dispose of the form after it is closed?

When you close Form , a WM_CLOSE message will be sent to the WM_CLOSE . If you look at the source code of the WmClose method that processes the WM_CLOSE message, you will see:

  • For modal forms (which you showed using ShowDialog ), the Dispose method will not be called, and the form will be closed after closing, and you can use its properties to get some data, or you can show it again.

  • For modeless forms (which you showed using Show ), after closing the form, the Dispose method will be called.

So here is the conclusion:

  • When you display a form using the Show method, you do not need (and you cannot) call Dispose . The form will be deleted after closing.

  • When you show the form using ShowDialog , you need to call Dispose manually. It is good practice to use modal forms in the using block.

+11
Sep 14 '16 at 23:31
source share

You can implement somekind a form manager that will subscribe to OnFormClosedEvent for each form that it displays, then it can destroy them ... something like:

 public class FormManager { public T ShowForm<T>() where T : Form, new() { var t = new T(); t.OnFormClosing += DisposeForm; return t; } void DisposeForm(object sender, FormClosedEventArgs args) { ((Form)sender).Dispose(); } } 

You can even go so far as to implement IDisposable and manage all non-zero forms when placing a manager :)

+1
Sep 14 '16 at 22:03
source share

Thus, according to MSDN's answer, modal forms are automatically deleted whenever you close them.

I decided to test this by opening my test form several times and closing it. I even opened several instances at the same time. After a second or two, the memory used by these forms was restored, indicating that they were correctly located.

0
Sep 14 '16 at 10:20
source share



All Articles