NSApplication.SharedApplication.Terminate doesn't actually terminate my application after unhandled exception

StackOverflow https://stackoverflow.com/questions/16039954

I am developing a simple application for OSX with MonoMac. When my application experiences unhandled exceptions, I catch them with:

AppDomain.CurrentDomain.UnhandledException += (sender, e) => Unhandled(e.ExceptionObject as Exception);

Inside the function Unhandled(), I log the exception and run a modal window to inform the user that something went wrong.

I then proceed to test this by throwing a regular Exception somewhere in the application. The crash report window appears as it should, and the error is logged.

But after this window has been closed, I would expect the application to quit, as it would have done if I hadn't caught the error. It does not; the application's main window and the dock icon are still there, but the window is completely frozen.

If I remove the above code line, the application closes completely when it crashes.

I try to force the application to quit by calling:

NSApplication.SharedApplication.Terminate(myAppDelegate);

But this seems to do nothing at all. I use the exact same code line in the application's Quit button, and there it works as expected, so there must be something special about the application state after catching an unhandled exception. But what?

I have overriden the following in my AppDelegate just in case, but it doesn't seem to make any difference:

public override NSApplicationTerminateReply ApplicationShouldTerminate (NSApplication sender)
{
    return NSApplicationTerminateReply.Now;
}

I thought that maybe displaying a modal form did something to the application state, but after testing I see that whether I show the modal form or not makes no difference. Still the frozen main window instead of closing.

EDIT: I must've done something wrong when testing this. It actually works as it should if I don't display the error form.

Any ideas?

有帮助吗?

解决方案

I don't have an answer for you exactly, and a comment wouldn't hold my code sample; but I can tell you that I'm conceptually doing almost exactly the same thing in my application and it works just fine. My code:

private static void OnException(Exception ex)
{
    new NSObject().InvokeOnMainThread(() => {
        try 
        {
            using (ErrorWindowController frmErrorMessage = new ErrorWindowController(ex)) 
            {
                Console.WriteLine(ex);
                    NSApplication.SharedApplication.RunModalForWindow(frmErrorMessage.Window);
            }
        } 
        finally 
        {
                NSApplication.SharedApplication.Terminate(NSApplication.SharedApplication);
        }
    });
}

EDIT:

Make sure you are stopping the modal session when you close the window, or the call to RunModalForWindow will never return.

I close my modal window using a button, something like this:

partial void butClose_Click(NSObject sender)
{
    this.Window.OrderOut(sender);
    this.Window.Close();

    NSApplication.SharedApplication.StopModal();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top