Do I need to Dispose a SemaphoreSlim

前端 未结 4 1206
梦毁少年i
梦毁少年i 2021-01-03 20:15

According to the documentation:

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

Are there any special

4条回答
  •  独厮守ぢ
    2021-01-03 20:16

    Yes.

    It may use a ManualResetEvent that uses a SafeWaitHandle which is a SafeHandle and it has an unmanaged handle.

    You can see it in the reference source here.

    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.

提交回复
热议问题