简体   繁体   中英

Weird unhandled exception with WPF

I have a WPF application with which I ask the user some settings to connect to a database, than I connect to the database (using NHibernate) and if everything is right I show my main view. If there is an error in the connection, I'd like to tell the user what is the error and let him retry. Here is some simplified code doing what I want:

EDIT:

It seems the problem isn't only with NHibernate. If I just run the simple app here, I get unhandled exception in the constructor.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        bool retry;

        do
        {
            retry = false;
            Window1 view = new Window1();

            try
            {
                throw new Exception("Test message");
                view.ShowDialog();
            }
            catch (Exception iException)
            {
                MessageBox.Show(iException.ToString());
                retry = true;
            }
            finally
            {
                view.Close();
            }
        }
        while (retry);
    }
}

I do get and unhandled exception and it gives me my test message so it really is my exception (even if it is inside a try/catch block). If I break when I get the exception it tells me it happens inside the constructor of Window1. Window1 doesn't contain any binding or control. It is just the basic Window1 that gets created if you create a new WPF Application in visual studio 2008. I have reproduced this bug on 2 computers (just create a new WPF application and paste this code in App.xaml.cs)

Thank you for your help everyone

I solved the problem by creating the window only once (before the loop). Than instead of closing it in the finally block I call Hide, and I close it only after the loop.

There could be any number of exceptions that could be happening and without posting the exception details, it's pretty hard to diagnose.

If I were to guess, my guess would be that view.Close() is throwing, since you say it's not being caught.

Try calling session.clear() in your catch statement.

Nhibernate will continue to buffer SQL until it writes to the database (flush). If you have a problem (exception), it won't throw it at the point when the exception occurs, but rather when the session gets flushed (when it tries to write the SQL to the DB).

Post the NHibernate exception, I could be way off on this..

You only need to build a session factory and a configuration ONCE. It can be a global object. Then, each time through the loop, have the session factory spin up a session. Since you are never destroying the session factory, building another one will screw up nhibernate because it only wants one session factory with one configuration.

With your code I get "System.InvalidOperationException: The Application object is being shut down." from the window - that seems to be the right exception. The window gets notified from app to close before it finishes instantiation while its resource is loading.

A solution to your problem is to wait with creation of your window until you know you didn't get an exception in Startup.

You could also change your finally part to dispatch the close instead so the window initialisation will finish before you close - I would try dispatcherpriority loaded.

If you run in release build you will get your never ending loop.

I don't believe the WPF runtime is ready to create windows during the OnStartup() method. That method is normally used to initialize the context of the application. The initial window is generally specified as the StartupUri in the tag in App.xaml.

Try refactoring your code to let Window1() be created by the runtime using the StartupUri instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM