C # 格式。关闭对格式。处理

我是 C # 的新手,我尝试查看早期的帖子,但没有找到一个好的答案。

在一个只有一个表单的 C # Windows 窗体应用程序中,使用 Form.Close()更好还是使用 Form.Dispose()更好?

MSDN 表示关闭对象中的所有资源,并在调用 Close 时释放窗体。尽管如此,我还是在网上看到了几个关于处置而不是结束的例子。

一个人比另一个人有优势吗? 在什么情况下,我们应该选择一个人而不是另一个人?

122932 次浏览

This forum on MSDN tells you.

Form.Close() sends the proper Windows messages to shut down the win32 window. During that process, if the form was not shown modally, Dispose is called on the form. Disposing the form frees up the unmanaged resources that the form is holding onto.

If you do a form1.Show() or Application.Run(new Form1()), Dispose will be called when Close() is called.

However, if you do form1.ShowDialog() to show the form modally, the form will not be disposed, and you'll need to call form1.Dispose() yourself. I believe this is the only time you should worry about disposing the form yourself.

Not calling Close probably bypasses sending a bunch of Win32 messages which one would think are somewhat important though I couldn't specifically tell you why...

Close has the benefit of raising events (that can be cancelled) such that an outsider (to the form) could watch for FormClosing and FormClosed in order to react accordingly.

I'm not clear whether FormClosing and/or FormClosed are raised if you simply dispose the form but I'll leave that to you to experiment with.

As a general rule, I'd always advocate explicitly calling the Dispose method for any class that offers it, either by calling the method directly or wrapping in a "using" block.

Most often, classes that implement IDisposible do so because they wrap some unmanaged resource that needs to be freed. While these classes should have finalizers that act as a safeguard, calling Dispose will help free that memory earlier and with lower overhead.

In the case of the Form object, as the link fro Kyra noted, the Close method is documented to invoke Dispose on your behalf so you need not do so explicitly. However, to me, that has always felt like relying on an implementaion detail. I prefer to always call both Close and Dispose for classes that implement them, to guard against implementation changes/errors and for the sake of being clear. A properly implemented Dispose method should be safe to invoke multiple times.

Using usingis a pretty good way:

using (MyForm foo = new MyForm())
{
if (foo.ShowDialog() == DialogResult.OK)
{
// your code
}
}

Close() - managed resource can be temporarily closed and can be opened once again.

Dispose() - permanently removes managed or not managed resource

If you use form.close() in your form and set the FormClosing Event of your form and either use form.close() in this Event ,you fall in unlimited loop and Argument out of range happened and the solution is that change the form.close() with form.dispose() in Event of FormClosing. I hope this little tip help you!!!

What I have just experiment with VS diagnostic tools is I called this.Close() then formclosing event triggered. Then When I call this.Dispose() at the end in Formclosing event where I dispose many other objects in it, it cleans everything much much smoother.

An old question, having some good answers, but I want to share a clear answer for this very popular question


How do I close Form in code?

  • Did you open the form using Show?

    Then to close it in code, just call Close().

  • Did you open the form using ShowDialog?

    Then close it by Close or by setting DialogResult.

Do I need to dispose Form?

  • Did you open the form using Show?

    No you don't need. After you close the form by Close or by clicking on X, it will be disposed automatically.

  • Did you open the form using ShowDialog?

    Yes you need. After you close the form by Close or by setting DialogResult or by clicking on X, if you are not going to reuse the form, you need to explicitly call Dispose, or make sure you create the form instance in a using block like this:

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

    If you want to reuse it later, then do not forget to explicitly Dispose in when you no more need it.