我是否需要处置SemaphoreSlim [英] Do I need to Dispose a SemaphoreSlim

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

问题描述

根据文档:


a SemaphoreSlim 不使用Windows内核信号。

"a SemaphoreSlim doesn't use a Windows kernel semaphore".

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

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