Make counters use dedicated thread instead of timer (#25864) (#25978)
* Make a dedicated polling thread for EventCounters
* Cleanup
* remove debug prints
* nit
* Fix comment
* addressing issues from code review
* Fix an assertion from getting fired when we have multiple listeners
* Fix a test issue
* code review feedback
* Fix a potential deadlock
* Fix another deadlock
* Allow s_sleepDurationInMilliseconds to be reset to a larger value
* More issues fix
* Let thread wake up from sleep if needed
* Some suggestions for the counters fix.
The resulting behavior should be the same as what is there now (assuming I didn't mess anything up), these are all just code simplifications that hopefully make it easier to review and maintain going forward. In total I think this reduces the change size in CounterGroup.cs by ~30%.
- With the addition of the AutoResetEvent the ManualResetEvent is no longer needed, removed it.
- Removed the various if foo != null checks for the shared state, it is all initialized once when then thread is created and then assumed to be non-null elsewhere.
- Removed a 2nd lock acquisition inside OnTimer
- Replaced an if with Math.Min in PollForValues
* fix test