I have written some applications of type Windows Forms Application
(GUI apps) and those applications can be launched/called from console instead of use the graphical interface, I mean it can be used both as GUI or as CLI.
The problem that I always have seen in this kind of "hybrid" apps is that a WinForms application that has CLI supports it takes a lot more time to initialize when the app is called from the CMD than a normal Console Application
, I suppose that this is because the need to load the (GUI) form.
...So, I would like to speed up the initialization/startup of my winforms apps when are used directly from the commandline.
At the moment I'm doing it through the Application.Startup event, but I would like to know if there is a more friendly way to avoid/suspend the Load event of the form when the alternative usage of the application tells that is not required to load a form because the app is gonna be used from the console.
To understand better my problem I will share some images...
This is the Graphical User Interface on my application:
http://img197.imageshack.us/img197/3397/kj5a.png
And this is the Command-line Interface:
When the program is called from the console it takes a lot of time to show the displayed console help because the application loads the form.
BUT AS I SAID I SOLVED THIS ISSUE BY USING THE APPLICATION.STARTUP EVENT:
Imports Reg2Bat.Main
Namespace My
Partial Friend Class MyApplication
Private Sub CLI() Handles MyBase.Startup
' Call the method that parses the CLI arguments (if any),
' this is done before loading the form to speed up the CLI startup.
ParseCLIArguments()
End Sub
End Class
End Namespace
...I just would like to know if exists a better way to avoid the form load event without touching the application events.
If you set the project startup method to
Sub Main
instead ofFormX
a static Sub Main (in a module) will run. You can write it as:to grab the commandline already parsed there. Presumably, if there IS a command line, you can go the CLI route, else instance and start in WinForms mode.
See also: https://stackoverflow.com/a/20301831/1070452
Alternative solution as said by @Plutonix:
At the project properties page uncheck "Enable application framework" to be able to load a Winforms (GUI) app from a module, otherwise I can't select any Main sub from a module.
And then write a module with the desired instructions to perform...for example: