So I've been plugging in some infrastructure for asset management, etc. In the process, I've improved on my old method of action/error logging. Along with various other customizations (recording various parameters and messages with errors), I wanted to be able to display a message box upon closing the app if there were errors recorded during the run. Also, if a fatal error was encountered, I wanted the app to exit straight away, but cleanly (Ie cleaning everything up and closing without exception boxes, etc).
For the exiting on fatal error business, I just have the app check (in the update() function) for a "isFatal" flag in my error logging class and call the this.Exit() function. Easy.
As for showing message boxes upon closing, that was a little trickier. It appears that any message boxes you create when the XNA app is closing don't get shown as the main window is disappearing. After looking around on the net a bit, I found the following solution. You spawn a seperate thread just for displaying the message boxes. Seems like a bit of overkill, but it works, and since the app is just closing anyway, there's no performance issue.
I created an exiting delegate to be called on closing the XNA game class:
this.Exiting += new EventHandler(Demo_Exiting);
Then I create the seperate thread if there are error messages to show in the delegate:
void Demo_Exiting(object sender, EventArgs e)
ThreadStart errorMsg = delegate
MessageBox.Show(OutputLog.fatalErrorMessage, "Fatal Error", MessageBoxButtons.OK,
string errorsReport = "Errors were reported.\n";
errorsReport += "Maximum error level was MINOR.\n";
errorsReport += "Maximum error level was MAJOR (not fatal).\n";
errorsReport += "Maximum error level was FATAL!.\n";
errorsReport += "See file errors.log for details.";
MessageBox.Show(errorsReport, "Errors reported", MessageBoxButtons.OK,
Thread thread = new Thread(errorMsg);
thread.IsBackground = false;
This works like a treat so now it reminds you if there were non-fatal errors encountered upon closing:
And you get some indication as to what happened if a fatal error as encountered, rather than just rudely dumping you out: