Application Architecture in Windows Forms 2.0—UI Thread Exceptions


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

Appl. Architecture Forms

© 2006 Pearson Education, Inc.

UI Thread Exceptions

One other application-level event that is fired as necessary by the Application object is the ThreadException event. This event is fired when a UI thread causes an exception to be thrown. This one is so important that Windows Forms provides a default handler if you don’t.

The typical .NET unhandled-exception behavior on a user’s machine yields a dialog, as shown in Figure 14.1.


Figure 14.1 Default .NET Unhandled-Exception Dialog

This kind of exception handling tends to make users unhappy. This dialog isn’t necessarily explicit about what actually happened, even if you view the data in the error report. And worse, there is no way to continue the application to attempt to save the data being worked on at the moment. On the other hand, a Windows Forms application that experiences an unhandled exception during the processing of an event shows a more specialized default dialog like the one in Figure 14.2.

Image:AAWFfig-14-2.jpg
Figure 14.2 Default Windows Forms Unhandled-Exception Dialog

This dialog is the ThreadExceptionDialog (from the System.Windows.Forms namespace), and it looks functionally the same as the one in Figure 14.1, with one important difference: The Windows Forms version has a Continue button. What’s happening is that Windows Forms itself catches exceptions thrown by event handlers; in this way, even if that event handler caused an exception—for example, if a file couldn’t be opened or there was a security violation—the user is allowed to continue running the application with the hope that saving will work, even if nothing else does. This safety net makes Windows Forms applications more robust in the face of even unhandled exceptions than Windows applications of old.

However, if an unhandled exception is caught, the application could be in an inconsistent state, so it’s best to encourage your users to save their files and restart the application. To implement this, you replace the Windows Forms unhandled-exception dialog with an application-specific dialog by handling the application’s thread exception event:

// Program.cs
static class Program {
 
 [STAThread]
 static void Main() {
  // Handle unhandled thread exceptions
  Application.ThreadException += App_ThreadException;
  ...
  // Run the application
  Application.Run(new MainForm());
 }
 
 static void App_ThreadException(
  object sender, ThreadExceptionEventArgs e) {
  // Does user want to save or quit?
  string msg = 
   "A problem has occurred in this application:\r\n\r\n" +
   "\t" + e.Exception.Message + "\r\n\r\n" +
   "Would you like to continue the application so that\r\n" +
   "you can save your work?";
  DialogResult res = MessageBox.Show(
   msg, 
   "Unexpected Error", 
   MessageBoxButtons.YesNo);
   ...
 }
}

Notice that the thread exception handler takes a ThreadExceptionEventArgs object, which includes the exception that was thrown. This is handy if you want to tell the user what happened, as shown in Figure 14.3.

Image:AAWFfig-14-3.jpg
Figure 14.3 Custom Unhandled-Exception Dialog

If the user wants to return to the application to save work, all you need to do is return from the ThreadException event handler. If, on the other hand, the user decides not to continue with the application, calling Application.Exit shuts down the application. Both are shown here:

// Program.cs
static class Program {
 ...
 static void App_ThreadException(
  object sender, ThreadExceptionEventArgs e) {
  ...
  // Save or quit
  DialogResult res = MessageBox.Show(...);
 
  // If save: returning to continue the application and allow saving
  if( res == DialogResult.Yes ) return;
 
  // If quit: shut ’er down, Clancy, she’s a’pumpin’ mud!
  Application.Exit();
}

Handling exceptions in this way gives users a way to make decisions about how an application will shut down, if at all, in the event of an exception. However, if it doesn’t make sense for users to be involved in unhandled exceptions, you can make sure that the ThreadException event is never fired. Call Application.SetUnhandledExceptionMode:

Application.SetUnhandledExceptionMode(
 UnhandledExceptionMode.ThrowException);

Although it’s not obvious from the enumeration value’s name, this code actually prevents ThreadException from being fired. Instead, it dumps the user straight out of the application before displaying the .NET unhandled-exception dialog from Figure 14.1:

namespace System.Windows.Forms {
 enum UnhandledExceptionMode {
  Automatic = 0, // default
  ThrowException = 1, // Never fire Application.ThreadException
  CatchException = 2, // Always fire Application.ThreadException
 }
}

In general, the behavior exhibited by UnhandledExceptionMode.ThrowException isn’t the most user friendly, or informative, when something catastrophic happens. Instead, it’s much better to involve users in deciding how an application shuts down.

Going the other way, you can also use command line arguments to let users make decisions about how they want their application to start up.


Previous_Page_.gif Next_Page_.gif


Personal tools