Do I need to call Close() on a ManualResetEvent?

前端 未结 6 958
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 11:43

I\'ve been reading up on .NET Threading and was working on some code that uses a ManualResetEvent. I have found lots of code samples on the internet. However, when reading

6条回答
  •  眼角桃花
    2020-12-10 12:44

    I was recently forwarded an excerpt from C# 4.0 in a Nutshell: The Definitive Reference By Joseph Albahari, Ben Albahari. On page 834, in Chapter 21: Threading there is a section talking about this.

    Disposing Wait Handles

    Once you’ve finished with a wait handle, you can call its Close method to release the operating system resource. Alternatively, you can simply drop all references to the wait handle and allow the garbage collector to do the job for you sometime later (wait handles implement the disposal pattern whereby the finalizer calls Close). This is one of the few scenarios where relying on this backup is (arguably) acceptable, because wait handles have a light OS burden (asynchronous delegates rely on exactly this mechanism to release their IAsyncResult’s wait handle).

    Wait handles are released automatically when an application domain unloads.

提交回复
热议问题