views:

9399

answers:

4

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.

A: 

No, if you always want to show that form.
Yes, if you sometimes want to use your app without GUI, just using command line.

Arvo
+2  A: 

Yes, and I have done it a few times.

One reason is, that if your app is COM EXE (speaking now from a VB6 point of view) then you want to be able to detect in what context the EXE is being called (being launched or being spoken to by some other app).

For example:

Sub Main()
    If App.StartMode = vbSModeAutomation Then
        ...
    Else
        ...
    End If
End Sub

Another is if you want your app to be able to handle any command line parameters.

For example:

Sub Main()
    If App.PrevInstance Then End
    If InStr(Command, "/s") > 0 Then
        Form1.Show
    ElseIf InStr(Command, "/p") > 0 Then
        LoadPicture ("c:\windows\Zapotec.bmp")
    End If
End Sub

(from one of my attempts to make a screen saver)

boost
IMHO COM EXE needs to started with Sub Main() anyway?
Arvo
You're right. I just checked what happens when you opt to create an ActiveX EXE in VB6 and it creates a class and drops you there to fill it up, rather than taking you to a new form.
boost
However, a COM EXE could be something with a form or two. One could have an EXE that shows a textarea which gets filled from a variety of different sources but way of COM.
boost
+6  A: 

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.

RoadWarrior
+1  A: 

You can do it either way, but you should really only keep code in the form that is directly related to the operations and user interface elements on that form. Application startup code isn't related to UI, normally concerned with splash screens, checking network connectivity, verifying a single instance only, setting up user configuration settings, and so on.

After the above items (or the appropriate initialization code for your app) are complete, Sub Main can create an instance of the main form, then show it so the user can begin interacting with your application.

This separates startup code from your form code. Later, when you're maintaining the application, you'll be glad you separated the two.

JeffK