I'm trying to load my AddIns for my application in another Thread. The process is time-consuming and the application freezes visibly long on startup. The system works as following:
A xml file defines the controls and types which should be added to the UI. A class now reads the XML and creates the controls. These are created by Invoking (using the main UI-Thread's Dispatcher).
In this case I'm creating a RibbonTabItem (Fluent Ribbon from Codeplex). Everything works fine until I add this control to the ribbon itself. Even this happens by invoking via the dispatcher. But as soon as this line get's called:
RibbonTabItem i = item;
uiDispatcher.Invoke(() => this._ribbon.Tabs.Add(i));
I get this exception stack trace:
at System.Windows.Freezable.EnsureConsistentDispatchers(DependencyObject owner, DependencyObject child) at System.Windows.Freezable.OnFreezablePropertyChanged(DependencyObject oldValue, DependencyObject newValue, DependencyProperty property) at System.Windows.Media.RenderData.PropagateChangedHandler(EventHandler handler, Boolean adding) at System.Windows.UIElement.RenderClose(IDrawingContent newContent) at System.Windows.Media.VisualDrawingContext.CloseCore(RenderData renderData) at System.Windows.Media.RenderDataDrawingContext.DisposeCore() at System.Windows.Media.DrawingContext.System.IDisposable.Dispose() at System.Windows.Media.RenderDataDrawingContext.Close() at System.Windows.UIElement.Arrange(Rect finalRect) at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize) at System.Windows.Controls.ContentPresenter.ArrangeOverride(Size arrangeSize) at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) at System.Windows.UIElement.Arrange(Rect finalRect) at System.Windows.Controls.Border.ArrangeOverride(Size finalSize) at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) at System.Windows.UIElement.Arrange(Rect finalRect) at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds) at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) at System.Windows.UIElement.Arrange(Rect finalRect) at System.Windows.Controls.Border.ArrangeOverride(Size finalSize) at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) at System.Windows.UIElement.Arrange(Rect finalRect) at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize) at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) at System.Windows.UIElement.Arrange(Rect finalRect) at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds) at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) at System.Windows.UIElement.Arrange(Rect finalRect) at Fluent.RibbonTabsContainer.ArrangeOverride(Size finalSize) at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) at System.Windows.UIElement.Arrange(Rect finalRect) at System.Windows.Controls.ScrollContentPresenter.ArrangeOverride(Size arrangeSize) at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) at System.Windows.UIElement.Arrange(Rect finalRect) at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize) at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) at System.Windows.UIElement.Arrange(Rect finalRect) at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds) at System.Windows.Controls.ScrollViewer.ArrangeOverride(Size arrangeSize) at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) at System.Windows.UIElement.Arrange(Rect finalRect) at System.Windows.Controls.DockPanel.ArrangeOverride(Size arrangeSize) at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect) at System.Windows.UIElement.Arrange(Rect finalRect) at System.Windows.ContextLayoutManager.UpdateLayout() at System.Windows.UIElement.UpdateLayout() at Fluent.RibbonTabControl.UpdateSelectedContent() at Fluent.RibbonTabControl.OnSelectionChanged(SelectionChangedEventArgs e) at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List
1 unselectedInfos, List
1 selectedInfos) at System.Windows.Controls.Primitives.Selector.SelectionChanger.End() at System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(ItemInfo info, Boolean assumeInItemsCollection) at System.Windows.Controls.Primitives.Selector.OnSelectedIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args) at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType) at System.Windows.DependencyObject.CoerceValue(DependencyProperty dp) at System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e) at Fluent.RibbonTabControl.OnItemsChanged(NotifyCollectionChangedEventArgs e) at System.Windows.Controls.ItemsControl.OnItemCollectionChanged2(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Controls.ItemCollection.OnViewCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.WeakEventManager.ListenerList1.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) at System.Collections.ObjectModel.ObservableCollection1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection
1.Add(T item) at FGCore.GUI.MenuSystem.RibbonUIHost.<>c_DisplayClass12.b_4() in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\Core\FGCore.GUI\MenuSystem\RibbonUIHost.cs:line 77 at System.Windows.Threading.Dispatcher.Invoke(Action callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout) at System.Windows.Threading.Dispatcher.Invoke(Action callback) at FGCore.GUI.MenuSystem.RibbonUIHost.SortHulls() in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\Core\FGCore.GUI\MenuSystem\RibbonUIHost.cs:line 77 at FGCore.GUI.MenuSystem.UIHost.Add(String id, FrameworkElement element, Int32 order, PropertiesCollection meta) in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\Core\FGCore.GUI\MenuSystem\UIHost.cs:line 54 at FGCore.Addin.Integrity.IntegrationPipeline.<>c_DisplayClass1a.b_13() in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\Core\FGCore.Addin\Integrity\IntegrationPipeline.cs:line 486 at System.Windows.Threading.DispatcherOperation.InvokeDelegateCore() at System.Windows.Threading.DispatcherOperation.InvokeImpl() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Windows.Threading.DispatcherOperation.Wait(TimeSpan timeout) at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherOperation operation, CancellationToken cancellationToken, TimeSpan timeout) at System.Windows.Threading.Dispatcher.Invoke(Action callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout) at System.Windows.Threading.Dispatcher.Invoke(Action callback) at FGCore.Addin.Integrity.IntegrationPipeline.ExtendHost(XmlElement subControl, UIHost host, IntegrationDescription desc, String path, Boolean creativeMode) in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\Core\FGCore.Addin\Integrity\IntegrationPipeline.cs:line 486 at FGCore.Addin.Integrity.IntegrationPipeline.ExtendRibbonHost(XmlElement hostNode, IntegrationDescription desc) in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\Core\FGCore.Addin\Integrity\IntegrationPipeline.cs:line 392 at FGCore.Addin.Integrity.IntegrationPipeline.ProcessExtensibility(IntegrationDescription description) in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\Core\FGCore.Addin\Integrity\IntegrationPipeline.cs:line 359 at FGCore.Addin.AddinProvider.RunAddin(Guid id, SettingsFile registry) in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\Core\FGCore.Addin\AddinProvider.cs:line 348 at FGCore.Addin.AddinProvider.RunAddins() in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\Core\FGCore.Addin\AddinProvider.cs:line 313 at FGCore.Addin.AddinCore.RunAddins() in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\Core\FGCore.Addin\AddinCore.cs:line 91 at ProjectFlowerGrid.IDE.b_1() in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\ProjectFlowerGrid.ApplicationBase\IDE.cs:line 253 at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at ProjectFlowerGrid.IDE.d_2.MoveNext() in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\ProjectFlowerGrid.ApplicationBase\IDE.cs:line 253 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__0(Object state) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at 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) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.Run() at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at ProjectFlowerGrid.App.Main() in d:\Sicherung\Visual Studio\Workspace\ProjectFlowerGrid\src\ProjectFlowerGrid.ApplicationBase\obj\Debug\App.g.cs:line 50 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(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) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
I already read that this could be caused by resources like Brushes which are created in the one thread and used in the other. Very plausible. But I really don't know how to fix this issue. If I have to freeze all resources, how could I do this without doing it manually for every Freezable?
Any other ideas?
Thanks :)