0

***I sincerely apologise as I should have posted the complete code.

I have an application where I have an IUIAutomationElementArray and I have cached data regarding bounding rectangles of each IUIAutomationElement from the array. I am then converting these to WPF borders

public class NumberRectangle : Border, IComparable
{

currently I am using iteration to convert the IUIAutomationElementArray to an array list of rectangles seen in the method declaration below.

 public ArrayList createRectangles(IUIAutomationElementArray elements)           
    {
        // create an array list to hold the rectangles
        rectArray = new ArrayList();                                                            
        for (int i = 0; i < elements.Length; i++)
        {
            IUIAutomationElement currentElement = elements.GetElement(i);
            //create DragonNumberRectangle to represent automation element
            NumberRectangle currentRectangle = new NumberRectangle(currentElement);
            //set horizontal and vertical alignment in order to align rectangles properly on window
            currentRectangle.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;             
            currentRectangle.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            currentRectangle.Height = (currentElement.CachedBoundingRectangle.bottom - currentElement.CachedBoundingRectangle.top);
            currentRectangle.Width = (currentElement.CachedBoundingRectangle.right - currentElement.CachedBoundingRectangle.left);
            // Thickness object represents Margin property of NumberRectangle (which is basically a Border)        
            Thickness rectThickness = new Thickness();
            //set Left and Top for position of rectangle
            rectThickness.Left = (currentElement.CachedBoundingRectangle.left);             
            rectThickness.Top = (currentElement.CachedBoundingRectangle.top);

            currentRectangle.Margin = rectThickness;
            // add colour rectangle to the list of rectangles
            rectArray.Add(currentRectangle);                                                                                                                                                            
        }
        //sort the rectangles to number from left to right/top to bottom
        rectArray.Sort();                                                                                     
        return rectArray;
    }    

I then draw the borders on a WPF window. The issue is that the createRectangles metho takes one second of processing time where the conversion of the element array to rectangles is the predominant time waster.

So the question is can I do this with Linq and how would I do this with Linq, an example would be great as I am not familiar with Linq currently. Perhaps the real question is how do I speed this up?

10
  • 4
    Why you still use ArrayList? There is no need to use it anymore, use generic lists. Commented May 4, 2018 at 12:32
  • 2
    What, exactly, makes you think that a LINQ solution would be faster? It would more than likely be slower Commented May 4, 2018 at 12:32
  • 5
    ArrayList is an artifact of .Net 1.0. You really should use the generic List<T> instead. Commented May 4, 2018 at 12:32
  • 1
    The likely place to increase performance is in the code you haven't shared, not in converting a for loop into Linq. Commented May 4, 2018 at 12:34
  • 2
    Your comments are simply unhelpful Ben, I know how to put borders on WPF elements. The question is very simple, what is the quickest way to convert the automation array to WPF borders. I didn't realise this forum contained cynics like you Ben Commented May 4, 2018 at 12:42

2 Answers 2

1

To achieve a conversion from array to arraylist you could simple do the following:

IUIAutomationElementArray elements = new IUIAutomationElementArray(); 

//populate that array with data

var arrayList = new ArrayList();
arrayList.Addrange(elements);

Ideally though you should be using a List<T> as opposed to an ArrayList, in which case you could do the following:

List<IUIAutomationElement> myList = new List<IUIAutomationElement>(elements);          

Here you are passing the array as a parameter in the constructor of the List.

Sign up to request clarification or add additional context in comments.

Comments

1

If you consider LINQ is more elegant than you can use conversion in this way. Surely this is a slower way to convert list of objects.

  ...
      rectArrayList =
            from e in elements
            select new
            {
                x = e.x,
                y = e.y,
                width = e.width,
                height = e.height,
            };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.