Start and Close a Windows Application in VB.NET
Application class provides Run and Exit static methods that can be used to start and close a Windows Forms applcation. This article discusses these methods, their roles and how to use them.
Run and Exit
Every Windows Forms application must have a main thread also known as primary thread that is executed when an application starts. An execution thread is a path of execution of code in an application. The main thread of an application starts by calling the Main method of an application. In .NET 4.0, the main thread is also called main message loop.
A Windows application may have at least one or more threads. Running multiple threads in a single application is also known as multi-threading. We discuss multi-threading in more details in the Threads and Processes chapter.
The entry point of a Windows application is run and ending point is close. In between run and exit, an application catches and handles Windows events occurred by accepting input devices as mouse clicks and keyboard strokes. The main message loop handles all the messages.
The Application class provides a Run method that starts an application. If you look at the Program.cs class in a Windows Forms application created by Visual Studio 2010, you will notice a Main method as listed in Listing 9.
static class Program
/// The main entry point for the application.
static void Main()
If you need to change the startup form of a Windows Forms application, you need to pass an instance of that Form class in the Application.Run method. Let's say, you have a Form called MasterForm and would like to open this form when application starts. To do so, you need to change the Application.Run() method call in Listing 9 to the following:
The following code snippet is VB.NET version of calling the Run method.
Application.Exit() method is used to close an application. This method notifies Windows that all messages for the current application must be terminated and all windows must be closed. You can use this method to close the application such as on Exit menu or Close button event handler.
The following code snippet is VB.NET version of calling the Exit method.
Programming Windows Forms using Visual Basic 2010 is a programming guide for Windows Forms developers using Visual Basic 2010 and Visual Studio 2010. This is an ongoing series. In this part, we learned about the Run and Exit methods of the Application class and how they are used in an application.