处理模式 [英] Dispose pattern

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

问题描述

我正在尝试理解IDisposable模式。在MSDN上的示例中,他们

添加了这样的析构函数/终结符:


~MyResource()

{

Dispose(false);

}


调用Dispose(false)只会处理非托管资源。如果我实现IDisposable只是为了处理托管资源而不是这个终结者

那么不必要吗?不会在GC上额外加入终结器吗?


MSDN示例可在此处找到:
http://msdn2.microsoft.com/en-us/lib...isposable.aspx


Thore Berntsen

I''m trying to understand the IDisposable pattern. In the sample on MSDN they
have added a destructor/finalizer like this:

~MyResource()
{
Dispose(false);
}

Calling Dispose(false) will only dispose non managed resources. If I
implement IDisposable only to dispose managed resources isn''t this finalizer
unecessary then? Wouldn''t the finalizer just at an extra burdon on the GC?

The MSDN sample is found here:
http://msdn2.microsoft.com/en-us/lib...isposable.aspx

Thore Berntsen

推荐答案

* Thore Berntsen写道,On 28-5-2007 12:03 :
* Thore Berntsen wrote, On 28-5-2007 12:03:

我正在尝试理解IDisposable模式。在MSDN上的示例中,他们

添加了这样的析构函数/终结符:


~MyResource()

{

Dispose(false);

}


调用Dispose(false)只会处理非托管资源。如果我实现IDisposable只是为了处理托管资源而不是这个终结者

那么不必要吗?不会在GC上额外加入终结器吗?


MSDN示例可在此处找到:
http://msdn2.microsoft.com/en-us/lib...isposable.aspx


Thore Berntsen

I''m trying to understand the IDisposable pattern. In the sample on MSDN they
have added a destructor/finalizer like this:

~MyResource()
{
Dispose(false);
}

Calling Dispose(false) will only dispose non managed resources. If I
implement IDisposable only to dispose managed resources isn''t this finalizer
unecessary then? Wouldn''t the finalizer just at an extra burdon on the GC?

The MSDN sample is found here:
http://msdn2.microsoft.com/en-us/lib...isposable.aspx

Thore Berntsen



这是正确的。但是最好实现你的Dispose来调用一个

保护的虚拟Dispose(bool)方法,无论

是否有终结器。如果你以后需要一个,你不需要改变太多。


Jesse


That is correct. But it is best to implement your Dispose to call a
protected virtual Dispose(bool) method regardless of the fact whether
have a finalizer. Should you need one later you won''t have to change much.

Jesse


Thore,


你说的非常正确,使用终结器肯定会增加开销,

所以如果你不需要它,就不要使用它。可终结的对象通常会被提升到更高的一代,因此会更长时间地保留在内存中,因为GC会花费更多时间在较低代中。这就是为什么

GC.SupressFinalize如此重要,如果

该对象已被处置,它将停止额外的工作。终结者也会在

单独的帖子中调用,所以如果你有任何

共享资源,你必须考虑到这一点。


关于Jesse所说的,我在更少的代码中是更好的阵营,所以如果你不是
不使用终结器我就不会为Dispose(bool处理)而烦恼

方法。

问候


Lee Alexander
www.feedghost.com

适用于Vista和RSS的RSS阅读器XP

同步,搜索,标记和分享。


" Thore Berntsen" < someone @ microsoft,comwrote in message

news:up ************** @ TK2MSFTNGP03.phx.gbl ...
Thore,

Your quite correct, there is certainly an added overhead using a finalizer,
so if you don''t need it, don''t use it,. Finalizable objects usually get
promoted to a higher generation and as a result stay in memory longer, as
the GC spends more of it''s time in the lower generations. This is why
GC.SupressFinalize is so important, it stops the extra work being done if
the object has already been disposed. Also the finalizer is called in a
seperate thread so you have to take that into consideration if you have any
shared resources.

Regarding what Jesse said, I''m in the less code is better camp, so if you
don''t use a finalizer I wouldn''t bother with the Dispose( bool disposing )
method.
Regards

Lee Alexander
www.feedghost.com
An RSS Reader for Vista & XP
Synchronize, search, tag and share.

"Thore Berntsen" <someone@microsoft,comwrote in message
news:up**************@TK2MSFTNGP03.phx.gbl...

我正在尝试理解IDisposable模式。在MSDN上的示例

中,他们添加了这样的析构函数/终结符:


~MyResource()

{

Dispose(false);

}


调用Dispose(false)只会处理非托管资源。如果我实现IDisposable只能处理托管资源,那么这个

终结器是不是必须的呢? GC上的终结器是不是只需要额外的burdon




这里可以找到MSDN示例:
http://msdn2.microsoft.com/en-us/lib。 ..isposable.aspx


Thore Berntsen
I''m trying to understand the IDisposable pattern. In the sample on MSDN
they have added a destructor/finalizer like this:

~MyResource()
{
Dispose(false);
}

Calling Dispose(false) will only dispose non managed resources. If I
implement IDisposable only to dispose managed resources isn''t this
finalizer unecessary then? Wouldn''t the finalizer just at an extra burdon
on the GC?

The MSDN sample is found here:
http://msdn2.microsoft.com/en-us/lib...isposable.aspx

Thore Berntsen



谢谢你们两位证实了这一点。我会删除我不必要的终结者

。其实我正在开发一个Compact Framework应用程序。 Althoug

GC在这里有点不同我仍然认为我应该使用

模式的完整框架。

Lee Alexander < lee @ feedghost_dot_comwrote in message

news:O1 ************** @ TK2MSFTNGP05.phx.gbl ...
Thank you both for confirming this. I wil remove my unecessary finalizers
rigth away. Actually I''m working on a Compact Framework application. Althoug
the GC is a bit different here I still think I should use the pattern from
the full framework.
"Lee Alexander" <lee@feedghost_dot_comwrote in message
news:O1**************@TK2MSFTNGP05.phx.gbl...

Thore,


你说的非常正确,使用

终结器肯定有额外的开销,所以如果你不需要它,不要用它。可终结的对象

通常被提升到更高的一代,因此留在内存中的时间更长,因为GC花费更多时间在较低代。这个

是GC.SupressFinalize如此重要的原因,如果对象已被处理掉,它会停止额外的工作

。终结者在一个单独的线程中称为

,所以如果你有
有任何共享资源,你必须考虑到这一点。


关于Jesse所说的,我在更少的代码中是更好的阵营,所以如果你不是
不使用终结器我就不会为Dispose(bool处理)而烦恼

方法。


问候


Lee Alexander
www.feedghost.com

用于Vista和RSS的RSS阅读器XP

同步,搜索,标记和分享。


" Thore Berntsen" < someone @ microsoft,comwrote in message

news:up ************** @ TK2MSFTNGP03.phx.gbl ...
Thore,

Your quite correct, there is certainly an added overhead using a
finalizer, so if you don''t need it, don''t use it,. Finalizable objects
usually get promoted to a higher generation and as a result stay in memory
longer, as the GC spends more of it''s time in the lower generations. This
is why GC.SupressFinalize is so important, it stops the extra work being
done if the object has already been disposed. Also the finalizer is called
in a seperate thread so you have to take that into consideration if you
have any shared resources.

Regarding what Jesse said, I''m in the less code is better camp, so if you
don''t use a finalizer I wouldn''t bother with the Dispose( bool disposing )
method.
Regards

Lee Alexander
www.feedghost.com
An RSS Reader for Vista & XP
Synchronize, search, tag and share.

"Thore Berntsen" <someone@microsoft,comwrote in message
news:up**************@TK2MSFTNGP03.phx.gbl...

>我正在尝试理解IDisposable模式。在MSDN上的示例中,他们添加了这样的析构函数/终结器:

~MyResource()
{
Dispose(false);
}

调用Dispose(false)只会处理非托管资源。如果我实现IDisposable只是为了处理托管资源那么这个
终结器是不是必须的呢? GC上的额外负载不会是终结器吗?

MSDN样本可以在这里找到:
http://msdn2.microsoft.com/en-us/lib...isposable.aspx

Thore Berntsen
>I''m trying to understand the IDisposable pattern. In the sample on MSDN
they have added a destructor/finalizer like this:

~MyResource()
{
Dispose(false);
}

Calling Dispose(false) will only dispose non managed resources. If I
implement IDisposable only to dispose managed resources isn''t this
finalizer unecessary then? Wouldn''t the finalizer just at an extra burdon
on the GC?

The MSDN sample is found here:
http://msdn2.microsoft.com/en-us/lib...isposable.aspx

Thore Berntsen




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

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