简体   繁体   English

WPF 的奇怪未处理异常

[英]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.我有一个 WPF 应用程序,我通过它询问用户一些设置以连接到数据库,然后我连接到数据库(使用 NHibernate),如果一切正常,我将显示我的主视图。 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.看来问题不仅在于 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).我确实得到了未处理的异常,它给了我我的测试消息,所以它确实是我的异常(即使它在 try/catch 块内)。 If I break when I get the exception it tells me it happens inside the constructor of Window1.如果我在收到异常时中断,它会告诉我它发生在 Window1 的构造函数中。 Window1 doesn't contain any binding or control. Window1 不包含任何绑定或控件。 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)如果您在 Visual Studio 2008 中创建新的 WPF 应用程序,它只是创建的基本 Window1。我已经在 2 台计算机上重现了这个错误(只需创建一个新的 WPF 应用程序并将此代码粘贴到 App.Z464BAAZ44B82951614C 中)

Thank you for your help everyone谢谢大家的帮助

I solved the problem by creating the window only once (before the loop).我通过只创建一次 window 解决了这个问题(在循环之前)。 Than instead of closing it in the finally block I call Hide, and I close it only after the loop.我没有在 finally 块中关闭它,而是调用了 Hide,并且仅在循环之后才关闭它。

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.如果我猜测,我的猜测是 view.Close() 正在抛出,因为你说它没有被抓住。

Try calling session.clear() in your catch statement.尝试在您的 catch 语句中调用 session.clear() 。

Nhibernate will continue to buffer SQL until it writes to the database (flush). Nhibernate 将继续缓冲 SQL 直到它写入数据库(刷新)。 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).如果您有问题(异常),它不会在异常发生时抛出它,而是在 session 被刷新时(当它试图将 SQL 写入 DB 时)。

Post the NHibernate exception, I could be way off on this..发布 NHibernate 异常,我可能会在这个..

You only need to build a session factory and a configuration ONCE.您只需要构建一个 session 工厂和一次配置。 It can be a global object.它可以是全局 object。 Then, each time through the loop, have the session factory spin up a session.然后,每次通过循环,让 session 工厂启动 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.由于您永远不会破坏 session 工厂,因此建造另一个工厂会搞砸 nhibernate 因为它只需要一个具有一种配置的 session 工厂。

With your code I get "System.InvalidOperationException: The Application object is being shut down."使用您的代码,我得到“System.InvalidOperationException:应用程序 object 正在关闭。” from the window - that seems to be the right exception.来自 window - 这似乎是正确的例外。 The window gets notified from app to close before it finishes instantiation while its resource is loading. window 在其资源加载时完成实例化之前从应用程序收到关闭通知。

A solution to your problem is to wait with creation of your window until you know you didn't get an exception in Startup.解决您的问题的方法是等待创建 window,直到您知道在启动中没有遇到异常。

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.您也可以更改您的 finally 部分以调度关闭,以便 window 初始化将在您关闭之前完成 - 我会尝试加载调度程序优先级。

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.我不相信 WPF 运行时已准备好在 OnStartup() 方法期间创建 windows。 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.初始的 window 一般在 App.xaml 的标签中指定为 StartupUri。

Try refactoring your code to let Window1() be created by the runtime using the StartupUri instead.尝试重构您的代码,让运行时使用 StartupUri 创建 Window1()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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