C# Form.Close 与 Form.Dispose [英] C# Form.Close vs Form.Dispose
问题描述
我是 C# 新手,我尝试查看之前的帖子,但没有找到好的答案.
I am new to C#, and I tried to look at the earlier posts but did not find a good answer.
在具有单个窗体的 C# Windows 窗体应用程序中,使用 Form.Close()
更好还是使用 Form.Dispose()
?
In a C# Windows Form Application with a single form, is using Form.Close()
better or Form.Dispose()
?
MSDN 说对象内的所有资源都被关闭,并且在调用 Close 时处理表单.尽管如此,我还是在网上遇到了几个遵循 Dispose 而不是 Close 的例子.
MSDN says that all resources within the object are closed and the form is disposed when a Close is invoked. Inspite of which, I have come across several examples online which follow a Dispose rather than a Close.
一个比另一个有优势吗?在哪些情况下我们应该更喜欢一种?
Does one have an advantage over the other? Under which scenarios should we prefer one over the other?
推荐答案
这个 MSDN 论坛告诉你.
This forum on MSDN tells you.
Form.Close()
发送正确的 Windows关闭 win32 的消息窗户.在这个过程中,如果形式未模态显示,处置是在表格上调用.处理表格释放非托管资源形式坚持.
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.
如果您执行 form1.Show()
或Application.Run(new Form1())
, Dispose将在 Close()
被调用时调用.
If you do a form1.Show()
or
Application.Run(new Form1())
, Dispose
will be called when Close()
is called.
但是,如果您执行 form1.ShowDialog()
以模态显示形式,形式不会被处理,你需要自己调用 form1.Dispose()
.一世相信这是你唯一的一次应该担心处理表格自己.
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.
这篇关于C# Form.Close 与 Form.Dispose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!