2010-04-15 3 views
2

예를 들어, Parallel.For를 사용하여 더 좋은 성능을 보일 수 있도록이 변환 방법이 있는지 궁금합니다.이 WPF 코드는 Parallel.For의 이점을 얻을 수 있습니까?

public FrameworkElement FindIntersectingElement(Rect rectangle, UIElement activeElement) 
{  
    foreach (var child in this.Children) 
    { 
     if (child != activeElement) 
     { 
      if (GetBounds(child as FrameworkElement, this).IntersectsWith(rectangle)) 
      { 
       return child as FrameworkElement; 
      } 
     } 
    } 

    return null; 
} 

public Rect GetBounds(FrameworkElement of, FrameworkElement from) 
{ 
    GeneralTransform transform = null; 

    transform = of.TransformToVisual(from); 

    return transform.TransformBounds(new Rect(0, 0, of.ActualWidth, of.ActualHeight)); 
} 

의견이 있으십니까?

답변

1

실제로 다음 테스트, 그래서 당신의 자신의 위험 (에 사용하지 않았다 - :.
내가 ActualWidth/높이를 읽는 것은 스레드 안전하다고 믿고있어

public FrameworkElement FindIntersectingElement(Rect rectangle, UIElement activeElement) 
    { 
     FrameworkElement found = null; 

     System.Threading.Tasks.Parallel.ForEach((IEnumerable<UIElement>)MainPanel.Children, 
      (child, loopState) => 
     { 
      if (child != activeElement) 
      { 
       if (GetBounds(child as FrameworkElement, MainPanel).IntersectsWith(rectangle)) 
       { 
        found = child as FrameworkElement; 
        loopState.Stop(); 
       } 
      } 
     }); 
     return found; 
    } 

그리고 제목에 응답 질의 : 어떤 속도 향상과 많은 중첩 요소가있을 때 가치가 있을지도 모릅니다.이 (트리 검색)은 드문 경우이지만 선형 개선보다 나은 결과를 얻을 수있는 드문 경우입니다.

+0

고맙습니다. 그리고 많은 수의 요소를 가지고 나는 실제로 속도 향상 (8 코어 머신에서 테스트)을 보았습니다. 단일 코어에 대한 선호도를 제한하면 differenc e도 마찬가지입니다. – TimothyP

+0

UIElementCollection이 제네릭 형식이 아니기 때문에 캐스트 (IEnumerable )가 작동한다고 생각하지 않습니다. –

+0

@Chris, Timothy는 이미 그것이 효과가 있다고보고했습니다. –

관련 문제