Tip Sheet: Application Events

How to use Application Framework Events

Application Framework events, introduced for VB.NET in VS 2005/Framework 2.0, provide you ways to better manage your application by extending the My namespace through partial classes.

To enable the Application Framework in your WinForms application, go to the Application tab of "My Project". Make sure the "Enable Application Framework" checkbox is checked. Then click on the "View Application Events" button in the frame below. This will create and show the ApplicationEvents module. Now you're ready to create and use the event procedures.

The first event we'll look at is the Startup event. This event is raised when the application starts and before the specified startup form is created. This allows you to execute code prior to any form loads. This is like the old Sub Main method but it acts as an extension of the My NameSpace. In this routine you can even show an optional setup dialog that you want seen before the main form in some cases. You can prevent the startup form from loading by using the Cancel property of the StartupEventArgs parameter.

The next event is the Shutdown event. This event allows you to run code while your application is in the process of shutting down. This event is good for saving final updates to the database, saving configuration information, or performing other final tasks. This is an ability that programmers coming from VB6 should particularly appreciate.

The UnhandledException event is another addition that VB6'ers will appreciate. Remember how you always asked how to have a 'global' error trap? Well, here it is. This event will fire whenever an untrapped error occurs in your program. While you should always manage the possibility of exceptions by writing good code and dealing with expected exceptions gracefully in your main code, this event does give your a safety valve to deal with truly unexpected situations. The UnhandledExceptionEventArgs parameter will give you the exception that happened and you can use it's ApplicationExit property to either return to your application if you set this value to False or you can use True, the default, to exit the application at this point.

StartupNextInstance is an event that fires if you've set the "Make Single Instance Application" of your application to True. This allows you to manage what happens when somebody tries to start another instance. You can use the StartupNextInstanceEventArgs BringToForeground property to pop your app to the front, a nice feature for the user who may have forgotten they already had it running. You can also use the CommandLine property of this parameter to get the command line from the other instance that was started. This is very handy if your program uses command lines.

The last event is NetworkAvailabilityChanged. This event, as the name implies, allows you to check if network connectivity is available. You use the NetworkAvailableEventArgs IsNetworkAvailable read-only boolean property to do this. In practice, this event is somewhat limited because it only detects basic connectivity. For example, if your computer is plugged into a hub but the connection beyond the hub goes down, this event won't fire. Therefore, in most cases, you would be better off using other methods to detect the network connectivity your application needs.

   Save to del.icio.us

Published on Wednesday, May 09, 2007   |   © 1999-2007 J. Frank Carr, All Rights Reserved