2012-03-06 7 views

답변

2

는 시도가

public class ObservableCollectionThreadSafe<T> : ObservableCollection<T> 
{ 
    // Override the event so this class can access it 
    public override event NotifyCollectionChangedEventHandler CollectionChanged; 

    public ObservableCollectionThreadSafe() 
    { 
    } 

    public ObservableCollectionThreadSafe(IEnumerable<T> items) 
     : base(items) 
    { 
    } 

    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) 
    { 
     // Be nice - use BlockReentrancy like MSDN said 
     using (BlockReentrancy()) 
     { 
      NotifyCollectionChangedEventHandler eventHandler = CollectionChanged; 
      if (eventHandler == null) 
       return; 

      Delegate[] delegates = eventHandler.GetInvocationList(); 

      // Walk thru invocation list 
      foreach (NotifyCollectionChangedEventHandler handler in delegates) 
      { 
       DispatcherObject dispatcherObject = handler.Target as DispatcherObject; 

       // If the subscriber is a DispatcherObject and different thread 
       if (dispatcherObject != null && dispatcherObject.CheckAccess() == false) 
       { 
        // Invoke handler in the target dispatcher's thread 
        dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e); 
       } 
       else // Execute handler as is 
        handler(this, e); 
      } 
     } 
    } 
} 

편집 : 그건 그렇고, 내 코드가, 그것은 누군가에 의해 인터넷에서 발견되었다 ... 그래서 "누군가"당신이 자신을 인식하는 경우, 당신은 그것을 위해 신용을해야 아니다 ...

+0

이전에 게시 한 코드를 사용해 본 결과 좋았습니다. 비록 지금은 비슷한 일을하고 싶지만 (업무상) VB.NET에 있어야하는 또 다른 프로젝트가 있습니다. VB는 변수처럼 사용되는 이벤트를 좋아하지 않는 것처럼 보입니다. "공용 그림자 이벤트 CollectionChanged (보낸 사람 개체, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)"이벤트이며 직접 호출 할 수 없습니다. 'RaiseEvent'문을 사용하여 이벤트를 발생시킵니다. " 어떻게 변환 할 생각인가요? – JamesMLV

+0

내가 더 봤어 야한다고 생각해. VB에서 (정상 변환 후)이 작업을하려면 eventname 다음에 "Event"를 추가하면됩니다. IntelliSense에는 나타나지 않지만 오류나 작동은 없습니다. – JamesMLV

관련 문제