Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can UI Automation be disabled for an entire WPF 4.0 app?

We are developing a WPF 4.0 application for internal use.
On some clients, we are experiencing huge performance issues due to UI automation (these clients have software installed like tablet service pen, touch, ..).

This is a known issue for WPF 4.0, see for instance:

  • WPF performance issue due to UI Automation
  • WPF UI Automation issue


We've been able to reproduce this issue on a machine with very limited specs. Opening a WPF window on this machine takes:

  • 00:00:02 - without any UI automation triggering software installed
  • 00:01:41 - with UI automation triggering software installed (RoboForm for this test)
  • 00:00:09 - with UI automation triggering software installed, and hotfix KB2484841 applied

As you can see, installing hotfix KB2484841 is a huge improvement, but still not as fast as running without ui Automation triggering software installed.
Furthermore, we do not have much control over which software to install at the clients, so it's hard to roll out this fix for all clients.


Therefore, is it possible to "turn off" UI Automation for an entire WPF application? I know it can be done on a per-UserControl basis, but is it possible for the app as a whole?

I've tried the code provided in this post, but without success.


Thanks for your time,
Koen

like image 320
KoenJ Avatar asked Sep 07 '25 06:09

KoenJ


1 Answers

We hit the exact same issue mentioned in the question, where a UI automation client was impacting the performance of our WPF application.

After trying all the hot fixes and workarounds, finally we found a solution. Each UI control has an AutomationPeer object that exposes the properties of the current control and its child controls. The UI automation client uses these AutomationPeer objects to get the information about the UI controls. There are built-in automation peer class for most of the UI controls in WPF and we can also create a custom peer class.

The following is a custom automation peer class. Note that in the GetChildrenCore method, it is returning an empty list instead of the list of actual child controls.

public class CustomWindowAutomationPeer : FrameworkElementAutomationPeer
{
    public CustomWindowAutomationPeer(FrameworkElement owner) : base(owner) { }

    protected override string GetNameCore()
    {
        return "CustomWindowAutomationPeer";
    }

    protected override AutomationControlType GetAutomationControlTypeCore()
    {
        return AutomationControlType.Window;
    }

    protected override List<AutomationPeer> GetChildrenCore()
    {
        return new List<AutomationPeer>();
    }
}

Then in your main window, override the OnCreateAutomationPeer method:

protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
{
    return new CustomWindowAutomationPeer(this);
}

Now when the UI automation client tries to get the child controls of the main window, it gets back an empty list and so it cannot iterate through the rest of the controls.

Refer this MSDN article for more details.

like image 183
ImthiyazPH Avatar answered Sep 08 '25 21:09

ImthiyazPH