Wumpf / VSSolutionColor

Visual Studio extension to change title bar color depending on the current solution
41 stars 18 forks source link

Automation Event handler causes system hangs #18

Closed Wumpf closed 5 years ago

Wumpf commented 5 years ago

Reported by Alois Kraus on VS Marketplace:

I have noticed that my system at random times becomes very slow and Task Manager, Explorer and things are mostly hanging. When I killed the VS Instances then everything recovered. After digging into the root cause (pretty complex stuff) I found that your extension creates a system wide UI Automation query which causes many NotifyWinEvent calls which when UI Automation is active in other UI Automation enabled processes causes sluggish system behavior. This happens more often when I have 3 or more VS instances open.

Please reconsider in SolutionColorPackage.cs the calls to

        // Sadly, it is not enough to listen to child windows of VS since code window popups are direct children of the desktop in terms of UI.
        Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, OnWindowOpenedClosed);
        // Weirdly, this doesn't apply to ALL child windows, and some are children of the main window after all. (Repro: Create a window out of two non-code views)
        var windowHandle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
        var windowAutomationElement = AutomationElement.FromHandle(windowHandle);
        Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, windowAutomationElement, TreeScope.Subtree, OnWindowOpenedClosed);

The UI Automation events seem to take system wide locks inside the Win32 subsystem and UI activity comes to a grinding halt when the event rate of UI Automation becomes too high. You should have experienced this as well. Now you know the root cause.