The Application object represents the running application at a very high level. It provides properties and methods for starting an event loop to process Windows messages, possibly for a form. It also provides methods for controlling and stopping the event loop.
Don’t confuse the Application object with the My.Application namespace. The two have somewhat similar purposes but very different features.
The following sections describe the Application object’s most useful properties, methods, and events.
The following table describes the Application object’s most useful properties.
PROPERTY | PURPOSE |
CommonAppDataPath | Returns the path where the program should store application data shared by all users. By default, this path has the form base_pathcompany_nameproduct_nameproduct_version. The base_path is typically C:Documents and SettingsAll UsersApplication Data. |
CommonAppDataRegistry | Returns the registry key where the program should store application data shared by all users. By default, this path has the form HKEY_LOCAL_MACHINESoftwarecompany_nameproduct_nameproduct_version. |
CompanyName | Returns the application’s company name. |
CurrentCulture | Gets or sets the CultureInfo object for this thread. |
CurrentInputLanguage | Gets or sets the InputLanguage for this thread. |
ExecutablePath | Returns the fully qualified path to the file that started the execution, including the filename. |
LocalUserAppDataPath | Returns the path where the program should store data for this local, non-roaming user. By default, this path has the form base_pathcompany_nameproduct_nameproduct_version. The base_path is typically C:Documents and Settingsuser_nameLocal SettingsApplication Data. |
MessageLoop | Returns True if the thread has a message loop. If the program begins with a startup form, this loop is created automatically. If it starts with a custom Sub Main, the loop doesn’t initially exist and the program must start it by calling Application.Run. |
OpenForms | Returns a collection holding references to all of the application’s open forms. |
ProductName | Returns the application’s product name. |
ProductVersion | Gets the product version associated with this application. |
StartupPath | Returns the fully qualified path to the directory where the program starts. |
UserAppDataPath | Returns the path where the program should store data for this user. By default, this path has the form base_pathcompany_nameproduct_nameproduct_version. The base_path is typically C:Documents and Settingsuser_nameApplication Data. |
UserAppDataRegistry | Returns the registry key where the program should store application data for this user. By default, this path has the form HKEY_CURRENT_USERSoftwarecompany_nameproduct_nameproduct_version. |
UseWaitCursor | Determines whether this thread’s forms display a wait cursor. Set this to True before performing a long operation, and set it to False when the operation is finished. |
To set the CompanyName, ProductName, and ProductVersion, open Solution Explorer, double-click the My Project entry, and select the Application tab. Then click the Assembly Information button and enter the values on the Assembly Information dialog box.
The following table describes the Application object’s most useful methods.
METHOD | PURPOSE |
AddMessageFilter | Adds a message filter to monitor the event loop’s Windows messages. |
DoEvents | Processes Windows messages that are currently in the message queue. If the thread is performing a long calculation, it would normally prevent the rest of the thread from taking action such as processing these messages. Calling DoEvents lets the user interface catch up with the user’s actions. Note that you can often avoid the need for DoEvents if you perform the long task on a separate thread. |
Exit | Ends the whole application. This is a rather abrupt halt, and any forms that are loaded do not execute their FormClosing or FormClosed event handlers. |
ExitThread | Ends the current thread. This is a rather abrupt halt, and any forms running on the thread do not execute their FormClosing or FormClosed event handlers. |
OnThreadException | Raises the Application object’s ThreadException event, passing it an exception. If your application throws an uncaught exception in the IDE, the IDE halts. That makes it hard to test Application.ThreadException event handlers. You can call OnThreadException to invoke the event handler. |
RemoveMessageFilter | Removes a message filter. |
Run | Runs a message loop for the current thread. If you pass this method a form object, it displays the form and processes its messages until the form closes. |
SetSuspendState | Makes the system suspend operation or hibernate. When the system hibernates, it writes its memory contents to disk. When you restart the system, it resumes with its previous desktop and applications running. When the system suspends operation, it enters low-power mode. It can resume more quickly than a hibernated system, but memory contents are not saved, so they will be lost if the computer loses power. |
The Application object provides a few events that give you information about the application’s state. The following table describes these events:
EVENT | PURPOSE |
ApplicationExit | Occurs when the application is about to shut down. |
Idle | Occurs when the application finishes executing some code and is about to enter an idle state to wait for events. |
ThreadException | Occurs when the application throws an unhandled exception. |
ThreadExit | Occurs when a thread is about to exit. |
When you end an application by unloading its form, the program receives the events FormClosing, FormClosed, ThreadExit, and ApplicationExit, in that order.
If you end the application by calling the Application object’s Exit method, the program only receives the ThreadExit and ApplicationExit events. If more than one thread is running, they each receive ThreadExit events, and then they each receive ApplicationExit events.