C# WPF应用程序崩溃

C# WPF应用程序崩溃,c#,wpf,tabcontrol,C#,Wpf,Tabcontrol,我正在使用TabControl(3个带有文本块、标签和数据网格的选项卡)开发一个WPF应用程序。应用程序使用后台线程自动刷新这些选项卡上的数据。一切都很好,除了一个事实:如果我在某个后台进程启动时切换选项卡,它会抛出“对象引用未设置为对象实例”错误并使应用程序崩溃。我已经设置了DispatcherUnhandledException来处理UI线程上任何未处理的异常。堆栈跟踪只提供了一个事实,即错误来自TabControl,可能来自与所选选项卡关联的对象,而没有提供太多数据。我的问题是:它们与WP

我正在使用TabControl(3个带有文本块、标签和数据网格的选项卡)开发一个WPF应用程序。应用程序使用后台线程自动刷新这些选项卡上的数据。一切都很好,除了一个事实:如果我在某个后台进程启动时切换选项卡,它会抛出“对象引用未设置为对象实例”错误并使应用程序崩溃。我已经设置了DispatcherUnhandledException来处理UI线程上任何未处理的异常。堆栈跟踪只提供了一个事实,即错误来自TabControl,可能来自与所选选项卡关联的对象,而没有提供太多数据。我的问题是:它们与WPF TabControl的固有问题是否可能导致这种行为?如前所述,我已经设置了尽可能多的错误处理,但收到的只是“对象引用…”错误

堆栈跟踪:

位于System.Windows.Controls.ItemContainerGenerator.MoveToPosition(GeneratorPosition位置、GeneratorDirection方向、布尔AllowStartRealizeItem、GeneratorState和state) 在System.Windows.Controls.ItemContainerGenerator.Generator..ctor(ItemContainerGenerator工厂、GeneratorPosition位置、GeneratorDirection方向、布尔AllowStartRealizedItem) 在System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.StartAt(GeneratorPosition位置,GeneratorDirection方向,布尔AllowStartRealizeItem) 位于System.Windows.Controls.VirtualzingStackPanel.MeasureOverride(大小约束) 在C:\dd\WPF\U 1\src\WPF\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\Primitives\DataGridRowsPresenter.cs中的Microsoft.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(大小约束):第118行 位于System.Windows.FrameworkElement.MeasureCore(可用大小大小) 位于System.Windows.UIElement.Measure(可用大小大小) 位于System.Windows.ContextLayoutManager.UpdateLayout()处 位于System.Windows.ContextLayoutManager.UpdateLayoutCallback(对象参数) 位于System.Windows.Media.MediaContext.InvokeRenderCallback.DoWork()处 在System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()中 位于System.Windows.Media.MediaContext.RenderMessageHandlerCore(对象大小调整的CompositionTarget) 位于System.Windows.Media.MediaContext.RenderMessageHandler(对象大小调整的CompositionTarget) 位于System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调、对象参数、布尔isSingleParameter) 位于System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源、委托回调、对象参数、布尔isSingleParameter、委托catchHandler) 位于System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调、对象参数、布尔isSingleParameter、委托catchHandler) 在System.Windows.Threading.DispatcherOperation.InvokeImpl()中 位于System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象状态) 位于System.Threading.ExecutionContext.runTryCode(对象用户数据) 在System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码、CleanupCode backoutCode、Object userData)中运行 位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext,ContextCallback回调,对象状态) 在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和handled) 在MS.Win32.HwndWrapper.WndProc(IntPtr-hwnd、Int32-msg、IntPtr-wParam、IntPtr-lParam、Boolean和handled) 在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o)上 位于System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调、对象参数、布尔isSingleParameter) 位于System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源、委托回调、对象参数、布尔isSingleParameter、委托catchHandler) 位于System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调、对象参数、布尔isSingleParameter、委托catchHandler) 位于System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority优先级、TimeSpan超时、委托方法、对象参数、布尔isSingleParameter) 位于System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority优先级、委托方法、对象参数) 位于MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam) 位于MS.Win32.unsafentivemethods.DispatchMessage(MSG&MSG) 位于System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架) 在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)上 在System.Windows.Threading.Dispatcher.Run()中 位于System.Windows.Application.RunDispatcher(对象忽略) 位于System.Windows.Application.RunInternal(窗口) 在System.Windows.Application.Run(窗口)中 在System.Windows.Application.Run()处 在C:\Users\jfeltis\Documents\Expression\Blend 3\Projects\salesforceConnect\salesforceConnect\obj\Debug\App.g.cs中的salesforceConnect.App.Main()处:第0行


所有这些对我来说都是胡言乱语。有没有比我更聪明的人可以看看,希望能给我一些指导?谢谢。

尝试在SelectionChanged事件处理程序中设置断点,并在应用程序启动后立即单击其他选项卡。在任何选项卡中都有UserControl吗?如果是这样,还要在加载的事件中放置断点。至少当您点击断点时,后台线程将暂停,您可以看到哪个对象为null


我使用的另一种蛮力方法是注释掉整个代码块,运行并测试,然后逐行逐渐取消注释,直到它中断。然后您至少知道一个潜在的崩溃位置。

您能在引发异常的行周围发布代码吗?没有代码我只是猜测,但我认为