Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF window crashes on startup, or it starts but hangs and does not render contents

Tags:

wpf

I've had a couple nasty problems with WPF applications crashing on certain client machines but working everywhere else - every developer's nightmare.

I'm posting the problems / solution here to help preserve the sanity of fellow developers. These were not easy to troubleshoot.

Both of these issues are classic "it works on my machine but not theirs" issues.

  1. WPF app crashes on startup. No errors, no idea why.
  2. WPF app starts up, the window border shows, but the contents do not paint. It just hangs (reported as a "transparent window") Clicking to close triggers a crash report (machine was windows XP).
like image 788
Jeffrey Knight Avatar asked Jan 30 '12 16:01

Jeffrey Knight


1 Answers

1) WPF app crashes on startup. No errors, no idea why.

The key troubleshooting step to actually find the error in the first place is to add additional error handling to app.xaml.

In your App.xaml header, add:

<Application DispatcherUnhandledException="App_DispatcherUnhandledException" />

and in your App.xaml.cs, add something like:

    void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs args)
    {
        log.Fatal("An unexpected application exception occurred", args.Exception);

        MessageBox.Show("An unexpected exception has occurred. Shutting down the application. Please check the log file for more details.");

        // Prevent default unhandled exception processing
        args.Handled = true;

        Environment.Exit(0);
    }

After adding this additional debugging, I caught the error:

System.TypeInitializationException: The type initializer for 'System.Windows.Media.FontFamily' threw an exception. ---> System.ArgumentException: Illegal characters in path.

Further research, googling, and coffee, led to the solution that the client machine had a trailing '◻' character in a font entry in the registry under

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts

Removing this rogue character solved the problem.

No idea how this got into the registry

See the career-saving blog post "How to Crash every WPF application".

2) WPF app starts up, the window border shows, but the contents do not paint. It just hangs (reported as a "transparent window")

This was another font-related issue. I initially boiled this down to a fontSize="16" in Window.xaml not working on one (and only one) machine at a client site, as if WPF refused to render their fonts at size 16 (?). I removed the code specifying the font size and it worked ... but as it turns out, it only worked for my login profile. It continued to fail for a colleague's login profile on the same machine machine. I could literally login as myself, run it successfully, log out, he'd log in as himself, run it, and it'd fail with the "transparent hanging window". Same machine, same permissions.

As a last resort, I logged in as him and noticed he had a custom theme set with enlarged font. I changed the windows theme back to the classic theme ... and this fixed the issue (?!?). This issue appears to also be font related, but I don't have an absolute root cause identified. Changing the theme back to standard theme is a temporary workaround.

like image 174
Jeffrey Knight Avatar answered Sep 20 '22 15:09

Jeffrey Knight