我有以下代码在 .NET 3.5 应用程序上创建一个新的 UI 线程。
private void OnCreateNewWindow(object sender, RoutedEventArgs e)
{
var rect = this.RestoreBounds;
double l = rect.Left;
double wth = rect.Width;
double t = rect.Top;
double ht = rect.Height;
var progressThread = new Thread(() =>
{
progressWindow = new ProgressWindow(Visibility.Collapsed) { Height = 50, Width = 50 };
progressWindow.WindowStartupLocation = WindowStartupLocation.Manual;
progressWindow.Left = l + (wth - progressWindow.Width) / 2;
progressWindow.Top = t - 35 + (ht - progressWindow.Height) / 2;
progressWindow.Show();
progressWindow.Activate();
Dispatcher.Run();
});
progressThread.SetApartmentState(ApartmentState.STA);
progressThread.Start();
}
此处演示了崩溃:
这在 32 位版本的 Windows 上完美运行。我第一次在 64 位 Windows 7 上重新启动 PC 时运行此程序时,如果通过 Visual Studio 运行或 Visual Studio 之外的应用程序崩溃,我会得到一个空异常。
异常细节是干的:
System.NullReferenceException was unhandled
堆栈跟踪如下:
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndWrapper.DestroyWindow(object args) + 0xfc bytes
WindowsBase.dll!MS.Win32.HwndWrapper.Dispose(bool disposing, bool isHwndBeingDestroyed) + 0xce bytes
WindowsBase.dll!MS.Win32.HwndWrapper.Dispose() + 0x15 bytes
PresentationCore.dll!System.Windows.Media.MediaContextNotificationWindow.DisposeNotificationWindow() + 0x22 bytes
PresentationCore.dll!System.Windows.Media.MediaContext.Dispose() + 0xba bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!System.Windows.Threading.Dispatcher.ShutdownImplInSecurityContext(object state) + 0x47 bytes
mscorlib.dll!System.Threading.ExecutionContext.runTryCode(object userData) + 0x178 bytes
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x62 bytes
WindowsBase.dll!System.Windows.Threading.Dispatcher.ShutdownImpl() + 0x87 bytes
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) + 0x121 bytes
> EMS.Controls.Dictionary.dll!EMS.Controls.Dictionary.Views.AuthenticateWindow.OnCreateNewWindow.AnonymousMethod() Line 56 + 0x5 bytes C#
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x9b bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x4d bytes
如果我继续再次运行该程序,我会从 Program Compatibility Assitant 收到一条消息,指示已应用了一些兼容性设置。下次我运行该程序时,我不再遇到此异常。有人经历过吗?