Sub Main (or Default Form)

Sub Main (or Default Form)

In Visual Basic .NET, your application s lifetime is now defined differently than it was in Visual Basic 6. When the startup object finishes, the application ends. In Visual Basic 6, an application remained active as long as there were dialog boxes or windows open. This represents a change in how your application s life cycle is determined. For the most part, however, you probably will not have to worry about this change, unless your application closes its main form but expects to continue running. The application model assumes the use of a main form that runs on the main thread. When that form terminates, your application terminates by default.

There are two main startup scenarios for Visual Basic applications. The first is when the startup object is a form. The second is when the startup object is a Sub Main method. Sub Main is typically used in two situations. You may implement a Sub Main method if you need to customize the loading process or if you used the Application Wizard to create your application.

note

The Upgrade Wizard attempts to treat Application Wizard projects as a special case, but if the Sub Main method is too heavily modified, it will not be able to do much for you. See Chapter 17for more on upgrading VB Application Wizard projects.

As a general rule, when you are opening a form from Sub Main, use the System.Windows.Forms.Application.Run method to start your application. This method begins running a standard Windows application messaging loop on the current thread and makes the specified form visible. If you re creating another form that may replace the original form, you should create it on a separate thread to ensure that your application does not terminate prematurely. Otherwise, when the specified form is closed, the application will terminate immediately. The following example demonstrates the use of the Application.Run method in a fictional Sub Main.

' This is how to start your application from Sub Main System.Windows.Forms.Application.Run(New Form1())



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net