Proper way to dispose a new Form

前端 未结 4 2040
囚心锁ツ
囚心锁ツ 2020-11-27 22:15

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



        
相关标签:
4条回答
  • 2020-11-27 22:54

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

    0 讨论(0)
  • 2020-11-27 23:06

    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.

    0 讨论(0)
  • 2020-11-27 23:10

    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();
    
    0 讨论(0)
  • 2020-11-27 23:12

    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.

    0 讨论(0)
提交回复
热议问题