Terminating a Windows Form Application

bdinnocenzo

Active member
Joined
Oct 22, 2009
Messages
38
Location
Massachusetts
Programming Experience
5-10
What is the proper way to terminate a Windows Form application while it is just executing in the MyApplication_Startup routine?

I have an application that looks for a database file upon startup. If it does not find it I prompt the user and ask if they wish to locate the file or not. If they choose not to locate the file I want to just terminate the application. Currently i just execute the "End" statement, but that does not seem like the proper way to terminate a program. Anyone know the proper way to do this?
 
End Statement is only recommended as a last resort to stop the app immediately if a clean app shutdown is not possible.
Startup event is cancellable. My.Application.Startup Event
help said:
You can use the Cancel property of the e parameter to control the loading of an application's startup form. When the Cancel property is set to True, the startup form does not start. In that case, your code should call an alternate startup code path.
VB.NET:
e.Cancel = True
The "alternate startup code path" mentioned gives a typical commandline operation example. If you have no additional operations I'd recommend you give the user some indication why nothing happens. In your case you are asking for alternate file location, which to me seems like valid feedback to why the app may terminate at that point.
 
I replace that "bad" End statement and set e.cancel = true and it ended up in a loop where it keeps asking to locate the "un-found" file. I think I did the right thing by adding an "Exit Sub" statement immediately after the e.cancel = true. This, I presume, exits the sub Application_Startup and because e.cancel is true it does not load the startup form, instead it terminates. Problem solved!

Thanks John! ;)
 
Back
Top