altmp / coreclr-module

CoreClr (.NET Core Common Language Runtime) community made module
MIT License
15 stars 7 forks source link

Fix HashSetEventHandler might cause EventStateManager to have incorrect handler count #8

Closed duydang2311 closed 5 months ago

duydang2311 commented 5 months ago

Consider this example:

Alt.OnKeyUp += Handler1;
Alt.OnKeyUp += Handler2;

Alt.OnKeyUp -= Handler1;
Alt.OnKeyUp -= Handler1; // yes, twice.

With the current behaviour, it will cause the HandlerCount of EventStateManager drops to 0, therefore the keyboard event will be turned off with Core_ToggleEvent and Handler2 will not be fired after that.

So, I think it would be better to add a check if HashSet<TEvent>.Add and HashSet<TEvent>.Remove returns a successful result before doing anything with EventStateManager.