简体   繁体   中英

What resources do AutoResetEvent / ManualResetEvent consume?

Are the c# ManualResetEvent and AutoResetEvent classes expensive to create or to maintain?

Do they consume some kind of limited Windows kernel resources, and if so, how limited is it?

Eg if I have code that can create a new AutoResetEvent every 100ms (to be disposed shortly afterwards), should I worry about putting old AutoResetEvents in a pool and reusing them, or is that not a significant concern?

Since they are IDisposables, I presume they consume some sort of limited resource. Just how much do they consume, and at which point should I start worrying about using too many of them?

The fact that there is a ManualResetEventSlim, but no AutoResetEventSlim also worries me a bit.

ManualResetEvent uses Wait Handles whereas ManualResetEventSlim uses Busy Spinning

The best performance, in order, is: 1) standard locking (Monitor) 2) "slim" event classes 3) standard event classes

Given your use case, I would recommend using the "slim" classes, since you will only be waiting for a short amount of time. Also, if a "slim" class waits for too long, it behaves as a "non-slim" class anyway.

Note you cannot use the "slim" classes across processes.


EDIT: This is why AutoResetEvent does not have a "slim" version - basically it's because the wait times of AutoResetEvent are typically longer than ManualResetEvent, so it isn't appropriate to use "busy spinning"


EDIT: A wait handle inherits from MarshalByRefObject. Ultimately .NET runtime sets up a proxy (TransparentProxy class) for remote access to your wait handle.

See here and here for more information.


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM