关于Dispose [英] About Dispose

查看:129
本文介绍了关于Dispose的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


一个一次性对象'的Dispose()方法可以由程序员明确地调用,也可以在完成时隐式调用。 />
如果您调用Dispose,则会提前释放非托管资源。

因此,如果您认为非托管资源很重要,请明确调用Dispose




我的问题是决定非托管资源的标准是什么

重要。


我想想......

文件很重要。

数据库连接很重要。


窗口句柄怎么样?

图形怎么样?


我想听听你对它的看法。


谢谢。


Sam

Hello!

A disposable object''s Dispose() method can be called either explicitly by
the programmer or implicitly during finalization.
If you call Dispose, the unmanaged resources are released earlier.
Thus, if you think the unmanaged resources are important, you call Dispose
explicitly.

My question is what''s the criteria to decide the unmanaged resources are
important.

I think...
files are important.
DB connections are important.

What about window handles?
What about Graphics?

I want to hear what you think about it.

Thanks.

Sam

推荐答案

Sam,


如果有问题的对象实现了IDisposable,然后你应该尽快打电话给

吧。这样做的原因是它代表了一些应该立即释放的资源(比如内存,文件

句柄,数据库连接等等)。


如果你让他们徘徊,那么它的真实,IDisposable方法的正确实现将确保他们*最终*得到清理,

但是为了保持你的应用程序(以及系统的其余部分正在运行)

有效,尽快调用Dispose方法符合你的最佳利益

,因为你已经完成了它们。


希望这会有所帮助。


" Sam Sungshik Kong" < ss*@chol.nospam.net>在消息中写道

news:uI ************** @ TK2MSFTNGP10.phx.gbl ...
Sam,

If the object in question implements IDisposable, then you should call
it as soon as you possibly can. The reason for this is that it represents
some resource that should be released right away (wether it is memory, file
handles, database connections, etc, etc).

If you let them linger around, then its true, a proper implementation of
the IDisposable method will make sure that they *eventually* get cleaned up,
but in order to keep your app (and the rest of the system running)
efficiently, it is in your best interests to call the Dispose method as soon
as you are done with them.

Hope this helps.

"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:uI**************@TK2MSFTNGP10.phx.gbl...
你好!

一次性对象的Dispose()方法可以由程序员明确调用,也可以在完成时隐式调用。
如果调用Dispose,则会更早释放非托管资源。因此,如果您认为非托管资源很重要,请明确调用Dispose

我的问题是确定非托管资源的标准是什么? 。

我认为...
文件很重要。
数据库连接非常重要。

窗口把手怎么样?
怎么样?图形?

我想听听你对它的看法。

谢谢。

Sam
Hello!

A disposable object''s Dispose() method can be called either explicitly by
the programmer or implicitly during finalization.
If you call Dispose, the unmanaged resources are released earlier.
Thus, if you think the unmanaged resources are important, you call Dispose
explicitly.

My question is what''s the criteria to decide the unmanaged resources are
important.

I think...
files are important.
DB connections are important.

What about window handles?
What about Graphics?

I want to hear what you think about it.

Thanks.

Sam




" Sam Sungshik Kong" < ss*@chol.nospam.net>在消息中写道

news:uI ************** @ TK2MSFTNGP10.phx.gbl ...

"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:uI**************@TK2MSFTNGP10.phx.gbl...
A一次性对象的Dispose()方法既可以由程序员明确调用,也可以在完成时隐式调用。


这是不正确的。调用终结器 - 而不是Dispose方法。


窗口句柄怎么样?
图形怎么样?
A disposable object''s Dispose() method can be called either explicitly by
the programmer or implicitly during finalization.
This is not correct. The finalizer is called -- not the Dispose method.

What about window handles?
What about Graphics?




如果您拥有对拥有非托管句柄的对象的托管引用,那么它可能实现了IDisposable。如果您对该实例拥有强大(和唯一)所有权

,则应实现IDisposable,以便您可以正确地投影处理模式

。在这种情况下实施

a终结器是没有意义的,因为最终化发生的时间太晚,你无法尝试清理这些实例。


OTOH,如果您有通过P / Invoke获得的原始非托管句柄或

其他方法,您应该在Dispose的代码路径中清理该句柄并且

你的终结者。在你的Dispose方法中,你应该取消定型。

-

Mickey Williams

作者,Visual C#.NET Core Ref, MS Press
www.neudesic.com
www.servergeek.com


嗨!

" Mickey Williams [C#MVP]" <我在servergeek dot com的名字>写在

消息新闻:uN ************** @ TK2MSFTNGP15.phx.gbl ...
Hi!

"Mickey Williams [C# MVP]" <my first name at servergeek dot com> wrote in
message news:uN**************@TK2MSFTNGP15.phx.gbl...

" ; Sam Sungshik Kong < ss*@chol.nospam.net>在消息中写道
新闻:uI ************** @ TK2MSFTNGP10.phx.gbl ...

"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:uI**************@TK2MSFTNGP10.phx.gbl...
一次性物品'' s Dispose()方法可以由程序员显式调用
,也可以在完成时隐式调用。
这是不正确的。调用终结器 - 而不是Dispose方法。
A disposable object''s Dispose() method can be called either explicitly by the programmer or implicitly during finalization.
This is not correct. The finalizer is called -- not the Dispose method.




我在MSDN中找到了这个语句。


因为Dispose方法必须明确调用,实现

IDisposable的对象还必须实现一个终结器来处理释放资源时

不调用Dispose。


我认为终结器是一个安全设备,当你没有
显式调用Dispose时。

当我说隐含地调用Dispose()时,我的意思是终结器

调用Dispose()。


我错过了什么吗?



I found this statement in MSDN.

Because the Dispose method must be called explicitly, objects that implement
IDisposable must also implement a finalizer to handle freeing resources when
Dispose is not called.

And I think that finalizer is a safety device for a case when you don''t
explicit call Dispose.
When I said that Dispose() is implicitly called, I meant that the finalizer
calls Dispose().

Am I missing something?

窗口句柄怎么样?
图形怎么样?
如果你有一个拥有非托管
What about window handles?
What about Graphics?
If you have a managed reference to an object that owns an unmanaged



句柄的对象的托管引用,它可能实现了IDisposable 。如果你对该实例拥有强大的(和唯一的)
所有权,你应该实现IDisposable,这样你就可以
正确地投射Dispose模式。在这种情况下,在
中实施终结器是没有意义的,因为最终化发生得太晚,你无法尝试清理这些实例。

OTOH,如果你有一个原始的您通过P / Invoke
或其他方式获得的非托管句柄,您应该在Dispose和
终结器的代码路径中清理该句柄。在您的Dispose方法中,您应该取消最终确定。

-
Mickey Williams
作者,Visual C#.NET Core Ref,MS Press
www.neudesic.com
www.servergeek.com



谢谢。


Sam


Thanks.

Sam


这篇关于关于Dispose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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