Proper way to dispose a new Form

十年热恋 提交于 2019-12-17 05:12:55

问题


So in my apps, I tend to create new instances of forms on the fly, then use Form.Show() to display them (non modal).

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

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

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

I don't want to use Form.ShowDialog(), because in a few instances I'm opening new windows that only show reports; I don't need them to be modal.


回答1:


Hmm, "code cracker" appears to be a very appropriate term for that tool, its advice certainly made you write code that breaks your program. Golden Rule is to never trust IDisposable advice from a static code analysis tool, none of them ever have sufficient insight in code execution. They can never figure out which Dispose() call gets the job done.

What it cannot see is that the Form class already knows how to dispose itself. It is very easy for it to do so, the object becomes unusable when the window closes. When there is no more window then there's no reason to keep using the Form object. A luxury that isn't otherwise very common in .NET but certainly inspired by very smart programmers that worked for Xerox 45 years ago.

There is only one special rule you have to keep in mind, it does not dispose itself when you use ShowDialog() to display the window. That was intentional, it makes retrieving the dialog results too risky. Using the using statement for a ShowDialog() call is very easy to do, the call does not return until the window is closed.




回答2:


Do I need to Dispose a Form after closing the Form?

When you show the form using Show() you don't need to dispose it, because it will dispose after close. But when you show form using ShowDialog(), you need to dispose the form, because it won't dispose after closing.

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

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

  • For non-modal forms (which you showed using Show), after the form got closed, the Dispose method will be called.

So here is the conclusion:

  • When you show a form using Show method you don't need to (and you can't) call Dispose. The form will be disposed itself after got closed.

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

Example

To show a modal dialog, as a best practice use a using block:

//form will be disposed after the using block
using (var f = new MyForm())
{
    if (f.ShowDialog() == DialogResult.OK)
    {
        //Your logic to handle OK here
    }
}

For a non-modal dialog, show and forget about it:

var f = new MyForm();
f.Show();



回答3:


You could implement somekind of forms manager that will subscribe to the OnFormClosedEvent for each form it shows, it can then dispose 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 could even go so far as to implement IDisposable and dispose all non-disposed forms when the manager is disposed :)




回答4:


So according to the answer over at MSDN, non modal forms are automatically Disposed whenever you close them.

I decided to test this by opening my test form multiple times and closing them. I even opened multiple instances at the same time. After a second or two, the memory used by these forms were reclaimed, which would indicate that they were being disposed properly.



来源:https://stackoverflow.com/questions/39500280/proper-way-to-dispose-a-new-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!