线程保存UIAutomation问题 [英] Thread save UIAutomation problem

查看:159
本文介绍了线程保存UIAutomation问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个控制台应用程序来自动化现有的GUI应用程序。当我尝试在GUI应用程序中选择树项时,我收到以下错误。



System.Runtime.InteropServices.COMException(0x8001010D):拨出电话不能因为应用程序正在调度输入同步调用。 (来自HRESULT的异常:0x8001010D(RPC_E_CANTCALLOUT_ININPUTSYNCCALL))



这是选择树项的代码:

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:

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);
            }





据我所知,错误正在发生,因为我正在从一个GUI应用程序进行更改不同的线程,我需要使用线程安全的实现来做到这一点。不幸的是,充其量我是一个线程新手,不明白如何最好地做到这一点。有人可以帮忙吗?



我通过加入自动化树行者来扩展代码。进行selectElement.select()调用时发生错误。



我查看了委托和Invoke方法,但我不知道如何调整control.invoke ()用于与AutomationElement一起使用。有没有什么可以将一个AutomationElement转换为一个控件,以便它可以访问调用?



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?

推荐答案

有一些信息可以给你详细的建议,但看起来你有一个线程同步问题。在c#中查找关键字Invoke和delegate。



尝试这篇文章 starters。
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.


UIAutomation对线程非常关键!



1.)你必须始终只从一个访问一个AutomationElement相同的线程。

2.)线程必须是STA线程(单线程公寓)



设置

thread.SetApartmentState(ApartmentState.STA);

在启动线程之前。



否则你可能会得到TreeWalker.GetFirstChild()的效果或者AutomationElement.FindAll()可能只返回一个AutomationElement子元素的一部分(例如它可能会返回10个子节点中的3个)



这个丢失的孩子bug非常多难以重现(并且可能发生在例如桌面而不是桌面上)



我可以在.NET应用程序中使用DataGrid控件重现此错误,其中只有一个返回10行的行。
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.


这篇关于线程保存UIAutomation问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆