This project has moved and is read-only. For the latest updates, please go here.

MessageBox ArgumentNullException

Nov 23, 2010 at 12:48 AM

Hi, I'm getting an ArgumentNullException when using the new MessageBox. This is happening on a call to MessageBox.Show(). Strangely, this only happens if I hit a breakpoint before Show() is called.

I have the calls to show encapsulated in a Module. Am I doing something wrong?

Here is the StackTrace:

Value cannot be null.
Parameter name: dependencyObject

   at System.Windows.Window.GetWindow(DependencyObject dependencyObject)
   at Microsoft.Windows.Controls.MessageBox.CreateContainer()
   at Microsoft.Windows.Controls.MessageBox.InitializeMessageBox(String text, String caption, MessageBoxButton button, MessageBoxImage image)
   at Microsoft.Windows.Controls.MessageBox.ShowCore(String messageText, String caption, MessageBoxButton button, MessageBoxImage icon)
   at Microsoft.Windows.Controls.MessageBox.Show(String messageText, String caption, MessageBoxButton button, MessageBoxImage icon)
   at Reporting.Client.DataVaultCentre.Dialogs.MessageDialogs.Show(String messageText, MessageBoxImage icon) in C:\Dev\Reporting\Client\DataVaultCentre\Reporting.Client.DataVaultCentre\Utils\MessageDialogs.vb:line 14
   at Reporting.Client.DataVaultCentre.Dialogs.MessageDialogs.ShowInformation(String messageText) in C:\Dev\Reporting\Client\DataVaultCentre\Reporting.Client.DataVaultCentre\Utils\MessageDialogs.vb:line 26
   at Reporting.Client.DataVaultCentre.ViewModels.VaultItemViewModel.SaveDataGridCompleted(Object sender, RunWorkerCompletedEventArgs e) in C:\Dev\Reporting\Client\DataVaultCentre\Reporting.Client.DataVaultCentre\ViewModels\VaultItemViewModel.vb:line 468
   at System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
   at System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)
   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.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   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.InvokeImpl(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 Reporting.Client.DataVaultCentre.Application.Main() in C:\Dev\Reporting\Client\DataVaultCentre\Reporting.Client.DataVaultCentre\obj\x86\Debug\Application.g.vb:line 76
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
   at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
   at System.Activator.CreateInstance(ActivationContext activationContext)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Nov 23, 2010 at 1:05 AM

I downloaded the source code and stepped through the code. The error seems to be in the method ResolveOwner. It loops over Application.Current.Windows to find the Active Window. As it turns out, none of the windows are active if the debugger has hit a break point?? Seems like there should be an easy way to fix this, like default to the main window if no active windows are found.

Nov 23, 2010 at 1:27 AM

Thanks for reporting this.  I will look into it.

Nov 23, 2010 at 3:06 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Nov 23, 2010 at 3:08 AM

This has been fixed and is available in the latest source code.

Mar 10, 2011 at 5:49 PM

I'd like to use this messageBox, but I've encountered the same null argument exception in the latest .NET 3.5 binaries.  Does the latest .NET 3.5 binaries download contain the fix applied in change set 64177?

I've even downloaded the source in an attempt to recompile and build the binaries myself, however I'm not able to find the source for the 3.5 solution.  It seems as though 3.5 is no longer supported.  Browsing through change set 64177 here: http://wpftoolkit.codeplex.com/SourceControl/changeset/view/64177# yields file structure sans the class files. 

I'm not in a position to change the target framework of my application at this time.  Might you have any advice?

Mar 10, 2011 at 6:09 PM

.NET 3.5 is still supported.  Recently I have made a major structure change in the solution structure and have not yet merged those changes into the 3.5 solution.  The current version 1.3.0 should not have this issue.  Could you please provide your code to reproduce this error?

Mar 10, 2011 at 7:16 PM

Preparing a sample app might be a bit too time consuming at the moment as this is a multi-tiered enterprise application involving a CAB shell application written in winforms hosting WPF user controls.   This means that it's a bit more complicated as the parent window is a winform window, not a WPF window.  I noticed the overloads for Show() do not include a parameter to pass the window owner.  It might be helpful in this case as I would be able to pass the parent window handle into the MessageBox.Show method for message box location deciphering.  If you have any ideas I'm happy to run through a few scenarios.

Mar 10, 2011 at 7:20 PM

I would recommend downloading the source associated with v1.3.0 and then step into the source code to see what is causing the issue.  I will add another overloaded Show method to pass in an owner.

Mar 10, 2011 at 7:21 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Mar 10, 2011 at 7:39 PM

I tried getting the source targeting 3.5, but the class files were not found in the source repository.  I found it strange, but you mentioned you're in the middle of a file structure overhaul.  I'm also not able to change my target framework due to the tangled complexity of the application I mentioned earlier.  I'll sit back and wait for the 3.5 source to be available and step through myself.  If the window handle (ptr) doesn't do the trick I'll let you know the results of my debug session when the 3.5 source is available in its entirety.

Mar 10, 2011 at 7:41 PM
Edited Mar 10, 2011 at 7:42 PM

You must download the entire Source Control structure.  The 3.5 solution links to the files in the 4.0 solution.  This allows me to more easily support both frameworks.

Download this entire change set:

http://wpftoolkit.codeplex.com/SourceControl/changeset/view/66611

Mar 11, 2011 at 12:23 AM

ResolveOwner() returns null as suspected.  If the CreateContainer() method didn't need to rely on the ResolveOwner() method to pass the parent window back then I think interop would be achievable. 

Mar 11, 2011 at 2:41 AM
Edited Mar 11, 2011 at 2:41 AM

well, it shouldn't matter if the owner is null, no error should be thrown.  The messagebox would just not have an owner.  Can you step into the code to see where you are getting the error?  Could you also add an overload to pass an owner to the Show method to test your theory?

Mar 11, 2011 at 5:50 PM

Stepping through the source does not throw the error.  In fact, I can successfully run with the source code when referencing the project directly.  However when referencing the released binary I get the exception.  In the released binary (1.3) it looks like the ResolveOwner method actually returns non-null, but when CreateContainer method tries to use that owner in the Window.GetWindow call it throws the exception.  Is the owner indeed null?   

What I've done is to package up the source code for release and reference that assembly rather than the source project itself and voila!