Search code examples
c#manualresetevent

Will the ManualResetEvent consume cpu while it is in a wait state?


More specifically, does the performance degradation of context switching apply to threads that are in a wait state?

Under what conditions or circumstances would a ManualResetEvent, or WaitHandle, be likely to consume resources?


Solution

  • A ManualResetEvent doesn't have a wait state. The only thing that can wait on an MRE is a thread. And yes, a thread consumes plenty of precious resources needlessly when it is not doing what it was made to do, execute code. A megabyte of virtual memory and a handful of kernel objects. The single kernel object that MRE consumes is small potatoes compared to that.

    You typically want to use a threadpool thread instead.

    And look at what's available in .NET 4.0. Like ManualResetEventSlim (not based on an OS object) and the Task class.