Click here to Skip to main content
15,394,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am attempting to write a console application to Automate an existing GUI application. I am getting the following error when I attempt to select a tree item in the GUI application.

System.Runtime.InteropServices.COMException (0x8001010D): An outgoing call cannot be made since the application is dispatching an input-synchronous call. (Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL))

Here is the code that selects the tree item:
C#
AutomationElement elementNode = TreeWalker.RawViewWalker.GetFirstChild(SensorTreeCollection);
            SelectionItemPattern selectElement;

            while (elementNode != null)
            {
                try
                {
                    Console.Out.WriteLine("node of type " + elementNode.Current.ControlType.LocalizedControlType + " with name of " + elementNode.Current.Name);
                    sw.WriteLine(elementNode.Current.Name);

                    try
                    {
                        selectElement = elementNode.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
                        selectElement.Select();
                        
                    }
                    catch (InvalidOperationException)
                    {
                        return;
                    }    
                }
                catch (Exception e)
                {
                    Console.Out.WriteLine("{0} Exception caught.", e);
                }
                elementNode = TreeWalker.ControlViewWalker.GetNextSibling(elementNode);
            }


From what I can tell the error is occurring because I'm making the change to the GUI application from a different thread and that I need to use a thread safe implementation to do this. Unfortunately, at best I am a threading novice and don't understand how best to do this. Can someone help?

I've expanded the code by including the automation treewalker. The error occurs when the selectElement.select() call is made.

I looked at delegates and the Invoke method but I don't know how to adapt control.invoke() for for use with an AutomationElement. Is there someway to cast an AutomationElement to a control so it can access invoke?
Posted
Updated 18-Nov-16 10:05am
v2

There is to little info to give you detailed advice but it look like you have a thread synchronization issue. Look up the keyword Invoke and delegate in c#.

try this article for starters.
   
Comments
crgmustang 26-Sep-14 19:26pm
   
Thanks for the help I added more of the code. I checked out delegates and the invoke method but its for a Control and not an AutomationElement. Is there someway to cast an AutomationElement to a control so I can access invoke?
Marc Koutzarov 29-Sep-14 12:15pm
   
http://msdn.microsoft.com/en-us/library/ms747211(v=vs.110).aspx
NEW ANSWER:

Do not use the .NET UIAUTOMATION anymore!
It is obsolete and buggy.
Please read my detailed answer here:
c# - System.Windows.Automation is extremely slow - Stack Overflow[^]

________________________________

OLD ANSWER:

UIAutomation is very thread-critical!

1.) You MUST access an AutomationElement always only from one and the same thread.
2.) The thread must be STA thread (Single Threaded Apartment)

Set
thread.SetApartmentState(ApartmentState.STA);
before starting the thread.

Otherwise you may get the effect that TreeWalker.GetFirstChild() or AutomationElement.FindAll() may return only a PART of the children of an AutomationElement (for example it may return 3 of 10 children)

This missing children bug is very difficult to reproduce (and may happen e.g. on a Table but not on a Pane)

I can reproduce this bug with a DataGrid control in a .NET application where only one row of 10 rows is returned.
   
v2
Comments
Dave Kreskowiak 18-Nov-16 17:09pm
   
You know this question is over TWO YEARS OLD, right? I doubt the OP is still looking for an answer.
Elmue 19-Nov-16 9:09am
   
And what is your problem?
You know that even in 10 years in the future people will STILL find this question by googleing?
Member 10481026 22-Nov-19 8:30am
   
I'm from 3 years in the futur thx for the answer
Member 15020806 8-Jan-21 13:22pm
   
I'm from even further in the future. Thanks for the answer. What should we be using instead of UI Automation?
Elmue 8-Jan-21 16:05pm
   
Well you just have to read the link on Stackoverflow that I posted above.
Roemeeeer 8-Apr-21 11:05am
   
I am the most futurest until now ;) You could use an abstraction library like FlaUI which usually solves a lot of problems when using UIA.
mikejobu23 22-Dec-20 12:44pm
   
Im from 4 years in the future, elmo still blows

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900