C#Form.Close VS Form.Dispose [英] C# Form.Close vs Form.Dispose

查看:322
本文介绍了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说,对象内的所有资源都关闭,并调用一个关闭时的形式布置。 Inspite其中,我也碰到过网上的几个例子它遵循的Dispose,而不是关闭。

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.

做一件有比其他优势?在何种情况下,我们应该preFER一个比其他?

Does one have an advantage over the other? Under which scenarios should we prefer one over the other?

推荐答案

<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/5cbf16a9-1721-4861-b7c0-ea20cf328d48\">This论坛在MSDN上告诉你。

This forum on MSDN tells you.

Form.Close()发送正确的窗户
  消息关停的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(新Form1的()),处置
  关闭()被调用时,将调用。

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 VS Form.Dispose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆