问题描述
根据文档:
SemaphoreSlim
是否使用任何特殊资源,因此对当 SemaphoreSlim
将不再使用时,调用处置
?
Are there any special resources used by the SemaphoreSlim
which make it important to call Dispose
when the SemaphoreSlim
will no longer be used?
推荐答案
是。
它可能使用 ManualResetEvent
SafeWaitHandle
是 SafeHandle
,它具有不受管理的句柄。
It may use a ManualResetEvent
that uses a SafeWaitHandle
which is a SafeHandle
and it has an unmanaged handle.
您可以在。
SafeHandle
是可最终确定的,因此,如果您不处置它(通过处置 SemaphoreSlim
),它将转到需要为您完成的终结器。由于终结器是单个线程,因此在某些情况下可能会工作过度,因此建议始终放置可终结对象。
SafeHandle
is finalizable so if you don't dispose of it (by disposing of the SemaphoreSlim
) it will go to the finalizer that will need to do that for you. Since the finalizer is a single thread it may get overworked in certain situations so it's always advisable to dispose finalizable objects.
这篇关于我是否需要处置SemaphoreSlim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!