Vb.net – Start VB.NET GUI app using Sub Main or form startup object

vb.netwinforms

Is there any reason to start a GUI program (application for Windows) written in VB.NET in the Sub Main of a module rather than directly in a form?

EDIT: The program won't take any command line parameters and it will be executed as a GUI program always.

Best Solution

The primary reason for using Main() in VB .NET 1.x was for adding code that needed to run before any forms were loaded. For example, you might want to detect whether an instance of your Windows Forms app was already loaded. Or you might want to intercept any unhandled exception for the AppDomain:

AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf MyExceptionFilter

But the next version of VB and Visual Studio 2005 introduced a new Application model that made Main() unnecessary in most scenarios. You can now intercept the My.Application.Startup event to add code that needs to run before any forms are loaded.

Note that the code for the Startup event handler is stored in the ApplicationEvents.vb file, which is hidden by default.

Related Question