2
AE_ComboBox = uia.GetElementByControlTypeAndAutomationId(globalSettings, ControlType.ComboBox, "1");
AE_TargetValue = uia.GetElement(AE_ComboBox, value, true);
SelectionItemPattern SIP = AE_TargetValue.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
SIP.Select();
Thread.Sleep(2000);
SIP.Current.IsSelected

This code throws an exception at the last line.

The target element corresponds to UI that is no longer available (for example, the parent window has closed).
at MS.Internal.AutomationProxies.Misc.ThrowWin32ExceptionsIfError(Int32 errorCode)
at MS.Internal.AutomationProxies.Misc.ProxySendMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Internal.AutomationProxies.WindowsListBox.ListboxItem.System.Windows.Automation.Provider.ISelectionItemProvider.get_IsSelected()

So, what could be the issue here?

Also, another question is, what does "current" mean in SelectionItemPattern? (I am sure it does not mean the current selected element.)

Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
user1947415
  • 933
  • 4
  • 14
  • 31

2 Answers2

1

I also faced the same problem. I was getting automationElement in one line, and accessing it through Current property in second line. `Current' property again goes to process of target application to get the value when you access. So during this cross process call if your ui element may no longer be available, this exception occurs. Even though you have automationElement object, it will be invalid.

If you just want to get the value, You can try using CacheRequet and then using Cached property of automationELement object .

Sanjay
  • 315
  • 3
  • 15
0

If you use WPR and the combo is not yet initialized, typically reading items from database which take long time, and accessing the combo you get this exception. So, you need to wait till the combo is initialized in a loop(and sleep maybe)