ARTICLE

Start and Close a Windows Application in VB.NET

Posted by Mahesh Chand Articles | Windows Forms VB.NET May 07, 2010
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.
 
Reader Level:

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

{

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

        Application.EnableVisualStyles();

        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new Form1());

    }

}

Listing 9

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: 

        Application.Run(new MasterForm())

 

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.

 

        Application.Exit();

The following code snippet is VB.NET version of calling the Exit method.

 

        Application.Exit()


Summary

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.

 

Programming Windows Forms using Visual Basic 2010

Here are previous articles related to this series.

share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Diagram
Become a Sponsor