WPF: Problem with menu's and GetWindow()

Mar 11, 2009 at 11:59 AM
I've wrote a sample application in WPF with multiple windows with a menu bar and i want to detect wich element is focused. So i subscribed to the focuschanged event of the ui automation library and i use white to run over all the windows to check where the element is located. When ik open a menu, the element is found but when i then ask a list of all the windows of the application i get the following error message:

Core.UIItems.UIActionException was caught
  Message="Window didn't respond, after waiting for 5000 ms"
  Source="Core"
  StackTrace:
       at Core.UIItems.WindowItems.Window.WaitWhileBusy() in d:\white-os\Core\UIItems\WindowItems\Window.cs:line 189
       at Core.UIItems.WindowItems.Window.InitializeWindow() in d:\white-os\Core\UIItems\WindowItems\Window.cs:line 54
       at Core.UIItems.WindowItems.Window..ctor(AutomationElement automationElement, InitializeOption initializeOption, WindowSession windowSession) in d:\white-os\Core\UIItems\WindowItems\Window.cs:line 49
       at Core.UIItems.WindowItems.WPFWindow..ctor(AutomationElement automationElement, WindowFactory windowFactory, InitializeOption initializeOption, WindowSession windowSession) in d:\white-os\Core\UIItems\WindowItems\WPFWindow.cs:line 15
       at Core.Factory.ChildWindowFactory.Create(AutomationElement element, InitializeOption option, WindowSession windowSession) in d:\white-os\Core\Factory\ChildWindowFactory.cs:line 35
       at Core.Factory.WindowFactory.DesktopWindows(Process process, ApplicationSession applicationSession) in d:\white-os\Core\Factory\WindowFactory.cs:line 58
       at Core.Application.GetWindows() in d:\white-os\Core\Application.cs:line 170
       at ScriptRecorder.GUI.RecorderManager.OnFocusChange(Object src, AutomationFocusChangedEventArgs e) in C:\Documents and Settings\Kristof\Desktop\ScriptPlayer\ScriptRecorder.GUI\RecorderManager.cs:line 53
  InnerException: System.NullReferenceException
       Message="Object reference not set to an instance of an object."
       Source="Core"
       StackTrace:
            at Core.UIItems.WindowItems.Window.WaitWhileBusy() in d:\white-os\Core\UIItems\WindowItems\Window.cs:line 172
       InnerException:

Anyone who knows what to do ?
Coordinator
Mar 22, 2009 at 12:35 PM
Eventhanding in UIA would generate event in another thread. Using AutomationElement objects from this thread would lead to issues like this. You should perform this operation from the main thread and not from event handling thread.