BlueMystical / Dark-Mode-Forms

Apply Dark Mode to all Controls in a Form [WinForms]
GNU General Public License v3.0
99 stars 13 forks source link

potential performance issues with switching theme multiple times #62

Open kachnitata opened 3 hours ago

kachnitata commented 3 hours ago

While the capability to switch the theme on the fly is a great improvement, I am not happy with the way it is implemented. When I try to switch dark/light theme multiple times, each time new event handlers are added. So, for example, when I put a breakpoint into the handler of a DataGridView

                grid.Paint += (object sender, PaintEventArgs e) => {... }

the first time it was executed once. but when I switched the theme, it was attached again and executed twice. And the next time three times, etc..

For controls that are nested deeper, this number can potentially grow as higher power of number of execution because of control.HandleCreated += , control.ControlAdded += etc. (not tested).

I think a better solution would be to extract all the handlers to methods or local functions and explicitly detach them before attaching them, like

            grid.Paint -= OnGridOnPaint;
            grid.Paint += OnGridOnPaint;

rewriting the code would of course take some time as there are many places like this...

kachnitata commented 3 hours ago

I can rewrite the code, piece by piece, when I find some spare time. But perhaps someone of you guys comes up with a better solution...

kachnitata commented 3 hours ago

also, the recently added ControlStatusStorage/ControlStatusInfo might be used to track whether the handlers have already been attached, and prevent adding them the second time.

on the beginning of ThemeControl() there is var info = controlStatusStorage.GetControlStatusInfo(control); so, we may add bool shouldAttachHandlers = info == null;

and later in the code, something like if(shouldAttachHandlers) grid.Paint += (object sender, PaintEventArgs e) => {... }

etc... but there are other handlers attached outside of this method....