Search code examples
c#wpfinitializationinvalidoperationexception

InvalidOperationException by InitializeComponent


If I open a new Window from the MainWindow Class, I got an InvalidOperationException. The exception is thrown by calling the InitializeComponent()-Method.

I don't know what to do.

ItemWindow itemWindow = new ItemWindow()
{
   WindowStartupLocation = WindowStartupLocation.CenterScreen,
   Topmost = true
};
itemWindow.ShowDialog();

The Error occur by calling the method at the second time.

The Message:

The Application object is being shut down.

Source: PresentationFramework

The StackTrace:

at System.Windows.Application.GetResourcePackage(Uri packageUri)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at Project.View.ItemWindow.InitializeComponent() in d:\Documents\Visual Studio 2012\Projects\Project\View\ItemWindow.xaml:line 1.
at Project.View.ItemWindow..ctor() in d:\Documents\Visual Studio 2012\Projects\ Project \View\ItemWindow.xaml.cs:line 45.
at Project.View.EditButton_OnClick(Object sender, RoutedEventArgs e) in d:\Documents\Visual Studio 2012\Projects\ Project \View\MainWindow.xaml.cs:line 479.

Edit: The problem occours only in the debug mode.

Edit2: The problem occours not only in the debug mode, also in the releas mode too.

The stack trace by clicking first time:

PresentationFramework.dll!System.Windows.Application.CriticalShutdown(int exitCode) Line 340
PresentationFramework.dll!System.Windows.Window.UpdateWindowListsOnClose() Line 4621
PresentationFramework.dll!System.Windows.Window.InternalDispose() Line 2380
PresentationFramework.dll!System.Windows.Window.WmDestroy() Line 4660
PresentationFramework.dll!System.Windows.Window.WindowFilterMessage(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Line 4349 + 0x7 Bytes
PresentationCore.dll!System.Windows.Interop.HwndSource.PublicHooksFilterMessage(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Line 1811 + 0x16 Bytes
WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Line 345 + 0x16 Bytes
WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Line 494 + 0x18 Bytes
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Line 111 + 0xa Bytes
WindowsBase.dll!MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(object source, System.Delegate method, object args, int numArgs, System.Delegate catchHandler) Line 41 + 0x17 Bytes
WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Line 1447 + 0x2d Bytes
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 398
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.DefWndProcWrapper(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 672 + 0x29 Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 424 + 0x1a Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.DefWndProcWrapper(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 672 + 0x29 Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 424 + 0x1a Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.DefWndProcWrapper(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 672 + 0x29 Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 424 + 0x1a Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) Line 2281 + 0x38 Bytes
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame frame) Line 368 + 0x9 Bytes
PresentationFramework.dll!System.Windows.Window.ShowHelper(object booleanBox) Line 5745 + 0xb Bytes
PresentationFramework.dll!System.Windows.Window.Show() Line 211 + 0x1d Bytes
PresentationFramework.dll!System.Windows.Window.ShowDialog() Line 456 + 0x7 Bytes
Test.exe!Test.FormMain.tab_Click(object sender, System.EventArgs e) Line 405 + 0xd Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs e) Line 8504 + 0xf Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs e) Line 278 + 0x9 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent) Line 308
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) Line 13171 + 0x274 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) Line 13920
System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m) Line 1308 + 0xa Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m) Line 397 + 0x7 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Line 14207 + 0xe Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) Line 14262 + 0xb Bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Line 779 + 0xe Bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) Line 2082
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) Line 3422 + 0x1b Bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) Line 3306 + 0xc Bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) Line 1495 + 0x31 Bytes
Test.exe!Test.Program.Main() Line 38 + 0x1d Bytes
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) Line 2031 + 0x18 Bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x27 Bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) Line 68 + 0x27 Bytes
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 581 + 0xd Bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 530 + 0xd Bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 519 + 0xe Bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() Line 105 + 0x20 Bytes
[Native to Managed Transition]

The stack trace by clicking second time:

PresentationFramework.dll!System.Windows.Application.GetResourcePackage(System.Uri packageUri) Line 2278
PresentationFramework.dll!System.Windows.Application.GetResourceOrContentPart(System.Uri uri) Line 2256 + 0x14 Bytes
PresentationFramework.dll!System.Windows.Application.LoadComponent(object component, System.Uri resourceLocator) Line 486 + 0x16 Bytes
Test.exe!Test.Testversandt.InitializeComponent() Line 1 + 0xb Bytes
Test.exe!Test.Testversandt.Testversandt(string html, string subject) Line 27 + 0x8 Bytes
Test.exe!Test.FormMain.tab_Click(object sender, System.EventArgs e) Line 404 + 0x21 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs e) Line 8504 + 0xf Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs e) Line 278 + 0x9 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent) Line 308
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) Line 13171 + 0x274 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) Line 13920
System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m) Line 1308 + 0xa Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m) Line 397 + 0x7 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Line 14207 + 0xe Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) Line 14262 + 0xb Bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Line 779 + 0xe Bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) Line 2082
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) Line 3422 + 0x1b Bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) Line 3306 + 0xc Bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) Line 1495 + 0x31 Bytes
Test.exe!Test.Program.Main() Line 38 + 0x1d Bytes
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) Line 2031 + 0x18 Bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x27 Bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) Line 68 + 0x27 Bytes
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 581 + 0xd Bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 530 + 0xd Bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 519 + 0xe Bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() Line 105 + 0x20 Bytes
[Native to Managed Transition]

Edit 3

I've found the reason.

The Main-application is written in windows forms. In a tab the application execute an elementhost with an WPF-UserControl. This UserControl opens the WPF-Window. By closing this WPF-Window, the WPF-UserControl is closed too. (Critical Shutdown)


Solution

  • This message means that your application started shutdown. Shutdown can be started by:

    1. Closing all windows if Application.ShutdownMode equals OnLastWindowClose.

    2. Closing main window if Application.ShutdownMode equals OnMainWindowClose.

    3. Calling shutdown manually.

    So check this situations because they cause problems.

    EDIT: Because you have the first listed reason you should change Application.ShutdownMode to OnExplicitShutdown.