Exemplo n.º 1
0
        protected override void StartLoop(NativeActivityContext context)
        {
            WindowsCacheExtension ext = context.GetExtension <WindowsCacheExtension>();
            var sw = new Stopwatch();

            sw.Start();
            Log.Selector(string.Format("Windows.GetElement::begin {0:mm\\:ss\\.fff}", sw.Elapsed));

            UIElement[] elements = null;
            var         selector = Selector.Get(context);

            selector = OpenRPA.Interfaces.Selector.Selector.ReplaceVariables(selector, context.DataContext);
            var sel        = new WindowsSelector(selector);
            var timeout    = Timeout.Get(context);
            var maxresults = MaxResults.Get(context);
            var minresults = MinResults.Get(context);

            if (maxresults < 1)
            {
                maxresults = 1;
            }
            var interactive = Interactive.Get(context);
            var from        = From.Get(context);
            int failcounter = 0;

            do
            {
                if (ClearCache != null && ClearCache.Get(context))
                {
                    Log.Selector(string.Format("Windows.GetElement::Clearing windows element cache {0:mm\\:ss\\.fff}", sw.Elapsed));
                    WindowsSelectorItem.ClearCache();
                }
                if (PluginConfig.get_elements_in_different_thread)
                {
                    elements = OpenRPA.AutomationHelper.RunSTAThread <UIElement[]>(() =>
                    {
                        try
                        {
                            Log.Selector(string.Format("Windows.GetElement::GetElementsWithuiSelector in non UI thread {0:mm\\:ss\\.fff}", sw.Elapsed));
                            return(WindowsSelector.GetElementsWithuiSelector(sel, from, maxresults, ext));
                        }
                        catch (System.Threading.ThreadAbortException)
                        {
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex.ToString());
                        }
                        return(new UIElement[] { });
                    }, PluginConfig.search_timeout).Result;
                }
                else
                {
                    Log.Selector(string.Format("Windows.GetElement::GetElementsWithuiSelector using UI thread {0:mm\\:ss\\.fff}", sw.Elapsed));
                    elements = WindowsSelector.GetElementsWithuiSelector(sel, from, maxresults, ext);
                    if (elements == null || elements.Length == 0)
                    {
                        elements = WindowsSelector.GetElementsWithuiSelector(sel, from, maxresults, ext);
                    }
                }
                //elements = WindowsSelector.GetElementsWithuiSelector(sel, from, maxresults);
                if (elements == null)
                {
                    elements = new UIElement[] { };
                }
                if (elements.Length == 0)
                {
                    Log.Selector(string.Format("Windows.GetElement::Found no elements {0:mm\\:ss\\.fff}", sw.Elapsed));
                    failcounter++;
                }
                if (failcounter > 2)
                {
                    WindowsSelectorItem.ClearCache();
                }
            } while (elements != null && elements.Length == 0 && sw.Elapsed < timeout);
            if (PluginConfig.get_elements_in_different_thread && elements.Length > 0)
            {
                // Get them again, we need the COM objects to be loaded in the UI thread
                elements = WindowsSelector.GetElementsWithuiSelector(sel, from, maxresults, ext);
            }
            context.SetValue(Elements, elements);

            var lastelements = context.GetValue(_lastelements);

            if (lastelements == null)
            {
                lastelements = new UIElement[] { }
            }
            ;
            context.SetValue(_lastelements, elements);
            if ((elements.Length + lastelements.Length) < minresults)
            {
                Log.Selector(string.Format("Windows.GetElement::Failed locating " + minresults + " item(s) {0:mm\\:ss\\.fff}", sw.Elapsed));
                throw new ElementNotFoundException("Failed locating " + minresults + " item(s)");
            }
            IEnumerator <UIElement> _enum = elements.ToList().GetEnumerator();
            bool more = _enum.MoveNext();

            if (lastelements.Length == elements.Length && lastelements.Length > 0)
            {
                more = !System.Collections.StructuralComparisons.StructuralEqualityComparer.Equals(lastelements, elements);
            }
            if (more)
            {
                if (interactive)
                {
                    var testelement = _enum.Current;
                    Wait.UntilResponsive(testelement.RawElement, PluginConfig.search_timeout);
                }
                context.SetValue(_elements, _enum);
                context.SetValue(_sw, sw);
                Log.Selector(string.Format("Windows.GetElement::end:: call ScheduleAction: {0:mm\\:ss\\.fff}", sw.Elapsed));
                IncIndex(context);
                SetTotal(context, elements.Length);
                context.ScheduleAction <UIElement>(Body, _enum.Current, OnBodyComplete);
            }
            else
            {
                Log.Selector(string.Format("Windows.GetElement:end {0:mm\\:ss\\.fff}", sw.Elapsed));
            }
        }