C# 在System.Windows.Interop.HwndSource.OnPreprocessMessage中调用ShowDialog()后出现NullReferenceException

C# 在System.Windows.Interop.HwndSource.OnPreprocessMessage中调用ShowDialog()后出现NullReferenceException,c#,.net,wpf,windows,exception,C#,.net,Wpf,Windows,Exception,我有一个WPF应用程序,有时在我调用窗口上的ShowDialog后应用程序会崩溃 当应用程序启动时,我打开一个主对话框,它总是打开的,然后我想打开一个新的子对话框,请参见下面的ShowModal方法,通常一切正常,但有时在调用ShowDialog(请参见下面的代码段)后,它会在方法System.Windows.Interop.HwndSource.OnPrepreprocessMessageObject参数中的PresentationFramework.dll中抛出System.NullRefe

我有一个WPF应用程序,有时在我调用窗口上的ShowDialog后应用程序会崩溃

当应用程序启动时,我打开一个主对话框,它总是打开的,然后我想打开一个新的子对话框,请参见下面的ShowModal方法,通常一切正常,但有时在调用ShowDialog(请参见下面的代码段)后,它会在方法System.Windows.Interop.HwndSource.OnPrepreprocessMessageObject参数中的PresentationFramework.dll中抛出System.NullReferenceException

调用ShowDialog后,窗口显示在应用程序中,然后当我单击窗口上的任何位置(例如文本框)时,它抛出NullReferenceException

我无法复制崩溃模式,它的行为完全随机。我开始发疯了,因为我不知道如何找出问题所在

这是我的代码片段:

private Window Wnd { get; set; }
public bool ShowModal(ViewModel vm)
{
    if( Wnd == null )
    {
        throw new InvalidOperationException( "Window was already closed" );
    }

    Wnd.DataContext = vm;
    Result = Wnd.ShowDialog().Equals(true);

    return Result;
 }
Wnd不是null,它包含我要显示的窗口的实例。在此行中引发异常

Result = Wnd.ShowDialog().Equals(true);
实际上在Wnd.ShowDialog之后

引发的异常快照:

System.NullReferenceException {"Object reference not set to an instance of an object."}
HResult = -2147467261
Message = Object reference not set to an instance of an object.
Data = {System.Collections.ListDictionaryInternal}
InnerExcpetion = null
Source = PresentationCore
StackTrace       
   at System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
   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 System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
   at System.Windows.Interop.HwndSource.OnPreprocessMessageThunk(MSG& msg, Boolean& handled)
   at System.Windows.Interop.HwndSource.WeakEventPreprocessMessage.OnPreprocessMessage(MSG& msg, Boolean& handled)
   at System.Windows.Interop.ThreadMessageEventHandler.Invoke(MSG& msg, Boolean& handled)
   at System.Windows.Interop.ComponentDispatcherThread.RaiseThreadMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Window.ShowHelper(Object booleanBox)
   at System.Windows.Window.Show()
   at System.Windows.Window.ShowDialog()
   at Intes.Utility.VisualizerService.UiWindow.ShowModal(ViewModel vm) in d:\Coder\Branch-AT\Source\Utilities\VisualizerService\Window abstraction\Implementation\UiWindow.cs:line 109


   // this part of call stack is not important, but I will leave it
   at A13.ViewModels.CalGroupExplorer.CalGroupExplorerPanelViewModel.NewMessage(IEnumerable`1 referenceIDs) in d:\Coder\Branch-AT\Source\A13\A13\ViewModels\CalGroupExplorer\CalGroupExplorerPanelViewModel.cs:line 159
   at A13.ViewModels.EntryViewModel.AddReferencedMessage() in d:\Coder\Branch-AT\Source\A13\A13\ViewModels\CalGroupExplorer\EntryViewModel.cs:line 147
   at A13.ViewModels.EntryViewModel.<get_AddReferencedMessageCommand>b__4(Object param) in d:\Coder\Branch-AT\Source\A13\A13\ViewModels\CalGroupExplorer\EntryViewModel.cs:line 136
   at Intes.Utility.Wpf.RelayCommand.Execute(Object parameter) in d:\Coder\Branch-AT\Source\Utilities\Wpf\RelayCommand.cs:line 70
   at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
   at System.Windows.Controls.MenuItem.InvokeClickAfterRender(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.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.Window.ShowHelper(Object booleanBox)
   at System.Windows.Window.Show()
   at System.Windows.Window.ShowDialog()
这看起来像是.NET Framework中的bug,我在A13和Intes项目中都使用了.NET Framework 4.5。A13是主项目,而Intes是引用的方法,ShowModal在项目Intes中的类中。A13编译为平台目标:x86,而Intes编译为平台目标:AnyCPU

我在x64处理器上的Windows 8.1上以调试模式运行应用程序,应用程序必须是x86


有人知道问题出在哪里吗?有人知道如何追踪问题吗?

OnPrepreprocessMessage只对击键做一些不寻常的事情。如果你的程序像这样在鼠标点击时爆炸,那么你有一个非常严重的腐败问题。必须是x86的要求确实暗示了本机代码可能会把事情搞砸。你在这里无法获得帮助,你需要记录一个小型转储并致电Microsoft支持寻求帮助。哦,等等,我不是那么准确。现在当我思考时,当我点击文本框并按下一个键后,它崩溃了。但正如我所说,它通常是有效的,有时会崩溃。我也没有注意到Windows 7 x64上的这种行为,但我不能100%确定,现在我正在尝试在Win 7上复制它,现在一切正常。我已经设法将项目编译为x64,但问题仍然发生。。。。。。在Win7 x64中,我的应用程序也会因相同的问题崩溃。我收到了一个用户关于此行为的报告:在文本框中单击并弹出,带有堆栈跟踪的NullReferenceException。。。而且它并不总是发生。。。这完全是随机的