WPF Dispatcher.Run在Win 7 64上抛出null异常

时间:2012-04-15 13:32:33

标签: c# .net wpf windows-7-x64

我有以下代码在.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();
        }         

此处演示了崩溃:enter image description here

这适用于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   

如果我再次继续运行程序,我会从程序兼容性助手那里收到一条消息,表明已经应用了一些兼容性设置。下次我运行程序时,我不再获得此异常。有人经历过吗?

1 个答案:

答案 0 :(得分:1)

此问题与应用程序启动顺序有关。正在单独的UI线程上创建进度窗口,以便可以在UI线程上完成对应用程序启动所需的Web服务的身份验证。看起来好像基于此thread on MSDN 在主UI线程上调用webservice方法是有问题的。

幸运的是,这是应用程序中唯一一个在UI线程上同步调用webservice方法的地方。

在对后台线程上的Web服务进行身份验证时,对主UI线程启动进度窗口进行更改后,到目前为止尚未发生问题。 有趣的是,在其他版本的Windows(包括Windows 7 x32)上没有出现此问题。