我怎样才能避免线。在使用WPF自动化框架时睡觉?

时间:2023-02-08 08:40:10

I am trying to use WPF UI Automation working without using spurious thread.sleep statements. What I would like to do is have a function GetElementById that continually polls until the control is available (or a timeout occurs). The problem is that it appears to cache the child controls of my parent element. Is it possible to refresh to Children? Or does anyone have an alternative approach?

我正在尝试使用WPF UI自动化,而不使用伪线程。睡眠语句。我想要做的是有一个函数GetElementById,它不断地轮询直到控件可用(或发生超时)。问题是它似乎缓存了父元素的子控件。有可能让孩子们清醒一下吗?或者有人有其他的方法吗?

public AutomationElement GetElementById(string id, int timeout)
{
    if (timeout <= 1000) throw new ArgumentException("Timeout must be greater than 1000", "timeout");

    AutomationElement element = null;

    int timer = 0;
    const int delay = 100;

    do
    {       
        element = MainWindow.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, id));                
        Thread.Sleep(delay);
        timer += delay;
    } while (element == null && timer < timeout);

    if (element == null) throw new InvalidOperationException("Unable to find element with id: " + id);

    return element;
}

2 个解决方案

#1


0  

If there is a better way than judicious use of Thread.Sleep, I've yet to find it.

如果有比明智地使用螺纹更好的方法的话。睡吧,我还没找到。

UIAutomation does provide Events, and I guess that you could (to use your case as an example) listen for the event that occurs when the TabItem is selected, and only then continue to find the item by Id. But I suspect that this kind of pattern would play havoc with the overall readability of your code.

UIAutomation确实提供了事件,我想,你可以(使用您的案件为例)监听事件,发生在TabItem被选中时,再继续寻找条目Id。但我怀疑这种模式会毁坏整个代码的可读性。

#2


0  

The DoEvents function that is available in System.Windows.Forms.Application namespace is used to enable the UI to update while a long running process is occuring. It is not a member of the WPF namespaces. You can use something sililar by implementing it yourself as in this article or importing the System.Windows.Forms namespace and calling Application.DoEvents. This will allow your other processing to continue while you are doing your polling. It is a function to be used with caution.

DoEvents函数,在System.Windows.Forms中可用。应用程序命名空间用于在长时间运行的进程发生时使UI更新。它不是WPF名称空间的成员。您可以像本文中那样通过自己实现或导入System.Windows来使用某些硅元素。表单命名空间和调用应用程序。这将允许在执行轮询时继续进行其他处理。它是一个要小心使用的函数。

#1


0  

If there is a better way than judicious use of Thread.Sleep, I've yet to find it.

如果有比明智地使用螺纹更好的方法的话。睡吧,我还没找到。

UIAutomation does provide Events, and I guess that you could (to use your case as an example) listen for the event that occurs when the TabItem is selected, and only then continue to find the item by Id. But I suspect that this kind of pattern would play havoc with the overall readability of your code.

UIAutomation确实提供了事件,我想,你可以(使用您的案件为例)监听事件,发生在TabItem被选中时,再继续寻找条目Id。但我怀疑这种模式会毁坏整个代码的可读性。

#2


0  

The DoEvents function that is available in System.Windows.Forms.Application namespace is used to enable the UI to update while a long running process is occuring. It is not a member of the WPF namespaces. You can use something sililar by implementing it yourself as in this article or importing the System.Windows.Forms namespace and calling Application.DoEvents. This will allow your other processing to continue while you are doing your polling. It is a function to be used with caution.

DoEvents函数,在System.Windows.Forms中可用。应用程序命名空间用于在长时间运行的进程发生时使UI更新。它不是WPF名称空间的成员。您可以像本文中那样通过自己实现或导入System.Windows来使用某些硅元素。表单命名空间和调用应用程序。这将允许在执行轮询时继续进行其他处理。它是一个要小心使用的函数。