UI元素在添加到控件集合时会引发异常

时间:2014-03-30 15:43:26

标签: c# .net wpf multithreading fluent-ribbon

我试图在另一个线程中为我的应用程序加载我的AddIns。该过程非常耗时,并且应用程序在启动时可能会很长时间冻结。系统的工作原理如下:

xml文件定义应添加到UI的控件和类型。类现在读取XML并创建控件。这些是由Invoking(使用主UI-Thread' Dispatcher)创建的。

在这种情况下,我创建了一个RibbonTabItem(来自Codeplex的Fluent功能区)。一切正常,直到我将此控件添加到功能区本身。甚至通过调度员调用也会发生这种情况。但是一旦这条线被称为:

RibbonTabItem i = item;
uiDispatcher.Invoke(() => this._ribbon.Tabs.Add(i));

我得到了这个异常堆栈跟踪:

  

在System.Windows.Freezable.EnsureConsistentDispatchers(DependencyObject owner,DependencyObject child)      在System.Windows.Freezable.OnFreezablePropertyChanged(DependencyObject oldValue,DependencyObject newValue,DependencyProperty property)      在System.Windows.Media.RenderData.PropagateChangedHandler(EventHandler处理程序,布尔添加)      在System.Windows.UIElement.RenderClose(IDrawingContent newContent)      在System.Windows.Media.VisualDrawingContext.CloseCore(RenderData renderData)      在System.Windows.Media.RenderDataDrawingContext.DisposeCore()      在System.Windows.Media.DrawingContext.System.IDisposable.Dispose()      在System.Windows.Media.RenderDataDrawingContext.Close()      在System.Windows.UIElement.Arrange(Rect finalRect)      在MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement元素,Size arrangeSize)      在System.Windows.Controls.ContentPresenter.ArrangeOverride(Size arrangeSize)      在System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)      在System.Windows.UIElement.Arrange(Rect finalRect)      在System.Windows.Controls.Border.ArrangeOverride(Size finalSize)      在System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)      在System.Windows.UIElement.Arrange(Rect finalRect)      在System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)      在System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)      在System.Windows.UIElement.Arrange(Rect finalRect)      在System.Windows.Controls.Border.ArrangeOverride(Size finalSize)      在System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)      在System.Windows.UIElement.Arrange(Rect finalRect)      在System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)      在System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)      在System.Windows.UIElement.Arrange(Rect finalRect)      在System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)      在System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)      在System.Windows.UIElement.Arrange(Rect finalRect)      在Fluent.RibbonTabsContainer.ArrangeOverride(Size finalSize)      在System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)      在System.Windows.UIElement.Arrange(Rect finalRect)      在System.Windows.Controls.ScrollContentPresenter.ArrangeOverride(Size arrangeSize)      在System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)      在System.Windows.UIElement.Arrange(Rect finalRect)      在System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)      在System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)      在System.Windows.UIElement.Arrange(Rect finalRect)      在System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)      在System.Windows.Controls.ScrollViewer.ArrangeOverride(Size arrangeSize)      在System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)      在System.Windows.UIElement.Arrange(Rect finalRect)      在System.Windows.Controls.DockPanel.ArrangeOverride(Size arrangeSize)      在System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)      在System.Windows.UIElement.Arrange(Rect finalRect)      在System.Windows.ContextLayoutManager.UpdateLayout()      在System.Windows.UIElement.UpdateLayout()      在Fluent.RibbonTabControl.UpdateSelectedContent()      在Fluent.RibbonTabControl.OnSelectionChanged(SelectionChangedEventArgs e)      在System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List 1 unselectedInfos, List 1 selectedInfos)      在System.Windows.Controls.Primitives.Selector.SelectionChanger.End()      在System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(ItemInfo info,Boolean assumeInItemsCollection)      在System.Windows.Controls.Primitives.Selector.OnSelectedIndexChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)      在System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)      在System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)      在System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)      在System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex,DependencyProperty dp,PropertyMetadata metadata,EffectiveValueEntry oldEntry,EffectiveValueEntry& newEntry,Boolean coerceWithDeferredReference,Boolean coerceWithCurrentValue,OperationType operationType)      在System.Windows.DependencyObject.CoerceValue(DependencyProperty dp)      在System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e)      在Fluent.RibbonTabControl.OnItemsChanged(NotifyCollectionChangedEventArgs e)      在System.Windows.Controls.ItemsControl.OnItemCollectionChanged2(Object sender,NotifyCollectionChangedEventArgs e)      在System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender,NotifyCollectionChangedEventArgs e)      在System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)      在System.Windows.Controls.ItemCollection.OnViewCollectionChanged(Object sender,NotifyCollectionChangedEventArgs e)      在System.Windows.WeakEventManager.ListenerList 1.DeliverEvent(Object sender, EventArgs e, Type managerType) at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at MS.Internal.Controls.InnerItemCollectionView.Insert(Int32 index, Object item) at System.Windows.Controls.ItemCollection.Insert(Int32 insertIndex, Object insertItem) at Fluent.Ribbon.OnTabsCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection 1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)      在System.Collections.ObjectModel.ObservableCollection 1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection 1.添加(T项)      在FGCore.GUI.MenuSystem.RibbonUIHost。<> c_ DisplayClass12.b _4()在d:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ Core \ FGCore.GUI \ MenuSystem \ RibbonUIHost.cs:第77行      在System.Windows.Threading.Dispatcher.Invoke(Action callback,DispatcherPriority priority,CancellationToken cancellationToken,TimeSpan timeout)      在System.Windows.Threading.Dispatcher.Invoke(Action回调)      在FGCore.GUI.MenuSystem.RibbonUIHost.SortHulls()在d:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ Core \ FGCore.GUI \ MenuSystem \ RibbonUIHost.cs:第77行      at FGCore.GUI.MenuSystem.UIHost.Add(String id,FrameworkElement element,Int32 order,PropertiesCollection meta)在d:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ Core \ FGCore.GUI \ MenuSystem \ UIHost.cs中:第54行      在FGCore.Addin.Integrity.IntegrationPipeline。<> c_ DisplayClass1a.b _13()在d:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ Core \ FGCore.Addin \ Integrity \ IntegrationPipeline.cs:第486行      在System.Windows.Threading.DispatcherOperation.InvokeDelegateCore()      在System.Windows.Threading.DispatcherOperation.InvokeImpl()   ---从抛出异常的先前位置开始的堆栈跟踪结束---      在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在System.Windows.Threading.DispatcherOperation.Wait(TimeSpan超时)      在System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherOperation操作,CancellationToken cancellationToken,TimeSpan超时)      在System.Windows.Threading.Dispatcher.Invoke(Action callback,DispatcherPriority priority,CancellationToken cancellationToken,TimeSpan timeout)      在System.Windows.Threading.Dispatcher.Invoke(Action回调)      在FGCore.Addin.Integrity.IntegrationPipeline.ExtendHost(XmlElement subControl,UIHost主机,IntegrationDescription desc,String path,Boolean creativeMode)中的d:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ Core \ FGCore.Addin \ Integrity \ IntegrationPipeline .cs:第486行      at FGCore.Addin.Integrity.IntegrationPipeline.ExtendRibbonHost(XmlElement hostNode,IntegrationDescription desc)在d:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ Core \ FGCore.Addin \ Integrity \ IntegrationPipeline.cs:第392行      在D:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ Core \ FGCore.Addin \ Integrity \ IntegrationPipeline.cs中的FGCore.Addin.Integrity.IntegrationPipeline.ProcessExtensibility(IntegrationDescription描述):第359行      在D:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ Core \ FGCore.Addin \ AddinProvider.cs中的FGCore.Addin.AddinProvider.RunAddin(Guid id,SettingsFile注册表):第348行      在D:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ Core \ FGCore.Addin \ AddinProvider.cs中的FGCore.Addin.AddinProvider.RunAddins():第313行      在FGCore.Addin.AddinCore.RunAddins()在d:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ Core \ FGCore.Addin \ AddinCore.cs:第91行      在ProjectFlowerGrid.IDE.b_ 1()在d:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ ProjectFlowerGrid.ApplicationBase \ IDE.cs:第253行      在System.Threading.Tasks.Task.InnerInvoke()      在System.Threading.Tasks.Task.Execute()   ---从抛出异常的先前位置开始的堆栈跟踪结束---      在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter.GetResult()      在ProjectFlowerGrid.IDE.d _2.MoveNext()在d:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ ProjectFlowerGrid.ApplicationBase \ IDE.cs:第253行   ---从抛出异常的先前位置开始的堆栈跟踪结束---      在System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__0(对象状态)      在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象args,Int32 numArgs)      在MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)      在System.Windows.Threading.DispatcherOperation.InvokeImpl()      at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)      at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)      在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,Int32 numArgs)      在MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)      在System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate方法,Object args,Int32 numArgs)      在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)      在MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)      在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)      在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)      在System.Windows.Threading.Dispatcher.Run()      在System.Windows.Application.RunDispatcher(Object ignore)      在System.Windows.Application.RunInternal(窗口窗口)      在System.Windows.Application.Run(窗口窗口)      在System.Windows.Application.Run()      在ProjectFlowerGrid.App.Main()的d:\ Sicherung \ Visual Studio \ Workspace \ ProjectFlowerGrid \ src \ ProjectFlowerGrid.ApplicationBase \ obj \ Debug \ App.g.cs:第50行      在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)      在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)      在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()      在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)      at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)      在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)      在System.Threading.ThreadHelper.ThreadStart()

我已经读过,这可能是由像在一个线程中创建并在另一个线程中使用的Brushes之类的资源引起的。非常合情合理。但我真的不知道如何解决这个问题。如果我必须冻结所有资源,我怎么能不为每个Freezable手动执行此操作?

还有其他想法吗?

谢谢:)

1 个答案:

答案 0 :(得分:0)

请原谅这个悲惨的错误。我忽略了我真的在并行线程中创建了freezable资源。这是一些刷子。我只是没有看到代码,因为它只是这个庞大类的一小部分。

我将它包装到UI调度程序的调用中,现在已经没有例外了。

再次,抱歉。我写这篇文章有点快。不会再发生了。