使用备用线程添加到可观察集合

时间:2011-09-20 16:13:46

标签: wpf multithreading observablecollection

我在备用线程中添加了一个observablecollection,这个集合绑定到wpftoolkit的数据网格。

当发生这样的事件时,oncollectionchanged正在通过主gui线程调用。

我看到的问题是应用程序抛出的大部分时间:

  

System.ArgumentOutOfRangeException未处理     消息=索引超出范围。必须是非负数且小于集合的大小。   参数名称:index     来源= mscorlib程序     PARAMNAME =指数     堆栈跟踪:          在System.ThrowHelper.ThrowArgumentOutOfRangeException()          at System.Collections.Generic.List 1.get_Item(Int32 index) at System.Collections.ObjectModel.Collection 1.System.Collections.IList.get_Item(Int32 index)          在System.Windows.Data.ListCollectionView.InternalItemAt(Int32索引)          在System.Windows.Controls.VirtualizingStackPanel.CleanupContainers(Int32 firstViewport,ItemsControl itemsControl)          在System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(大小约束)          在C:\ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ Primitives \ DataGridRowsPresenter.cs中的Microsoft.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(大小约束):第109行          在System.Windows.FrameworkElement.MeasureCore(Size availableSize)          在System.Windows.UIElement.Measure(Size availableSize)          在System.Windows.ContextLayoutManager.UpdateLayout()          在System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)          在System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()          在System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)          在System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)          在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象args,布尔isSingleParameter)          at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,Delegate callback,Object args,Boolean isSingleParameter,Delegate catchHandler)          在System.Windows.Threading.DispatcherOperation.InvokeImpl()          在System.Threading.ExecutionContext.runTryCode(Object userData)          在System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码,CleanupCode backoutCode,Object userData)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Windows.Threading.DispatcherOperation.Invoke()          在System.Windows.Threading.Dispatcher.ProcessQueue()          在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& handling)          在MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& handling)          在MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)          在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象args,布尔isSingleParameter)          at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,Delegate callback,Object args,Boolean isSingleParameter,Delegate catchHandler)          在System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate方法,Object args,Boolean isSingleParameter)          在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)          在MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)          在System.Windows.Threading.Dispatcher.TranslateAndDispatchMessage(MSG& msg)          在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)          在System.Windows.Application.RunInternal(窗口窗口)          at Apollo.App.Main()在F:\ Workspaces \ BVS \ BVS \ Apollo \ Apollo \ _ obj \ Debug \ App.g.cs:第0行

有关这可能是什么的任何想法?这是oncollectionchanged代码:

protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        using (BlockReentrancy())
        {
            KeyValuePair<NotifyCollectionChangedEventHandler, CollectionChangedEventData>[] handlers = _collectionChangedHandlers.ToArray();

            if (handlers.Length > 0)
            {
                foreach (KeyValuePair<NotifyCollectionChangedEventHandler, CollectionChangedEventData> kvp in handlers)
                {
                    if (kvp.Value.Dispatcher == null)
                    {
                        kvp.Value.Action(e);
                    }
                    else
                    {
                        kvp.Value.Dispatcher.Invoke(kvp.Value.Action, DispatcherPriority.DataBind, e);
                    }
                }
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

该堆栈跟踪的顶部表明ListCollectionView中的普通List<>出现了问题。

您可能应该查看绑定到SelectedItem等的代码。