|
-
Sep 20th, 2006, 11:25 PM
#1
Thread Starter
Junior Member
Exiting Program
Hi everyone.
I've written a program that uses data from an external file. On startup, it checks if the file is there, and is supposed to exit if it isn't.
I tried using both Application.Exit() and System.Windows.Forms.Application.Exit() but for some reason the code keeps executing and I get a NullReference exception (the code loads the data into arrays if it is present).
Now if I used the End command, it exits fine. I was taught never to use things like break, end, etc outside of case statements. Is there any reason why? Is there any easy way of solving my problem? I know I could probably stick the whole program inside a while loop & put in a terminating condition to exit, but I'd rather not resort to this if there's an easier way.
Thanks,
dabossss
-
Sep 20th, 2006, 11:43 PM
#2
Re: Exiting Program
Don't use End. I take it that this is a Windows Application project. You should probably check for the existence of this file before even creating a form. If the fiel isn't there then just don't create a form and the app will exit on its own without you lifting a finger. You haven't specified which version you're using. Please do so for every thread because the solution to a problem is often different for each, as it is here. Specify your version and you'll get the appropriate solution for that version.
-
Sep 21st, 2006, 12:06 AM
#3
Thread Starter
Junior Member
Re: Exiting Program
Sorry - I rushed into making this thread so didn't put it in the right subforum.
I'm using Visual Basic Express 2005.
This may sound like a newbie question, but how do I check the existance without the form loading? How can I have a method running outside the form?
Also, why does the program keep executing code after I try to exit? Is System.Windows.Forms.Application.Exit() the same as Application.Exit()?
Is it considered bad practice to use System.Environment.Exit(0)? This seems to work ok...
-
Sep 21st, 2006, 12:23 AM
#4
Re: Exiting Program
You're in the right forum. VB 2005 is VB.NET, just the latest version of it from which Microsoft have dropped the (now assumed) .NET suffix. In VB 2005 the Startup application event is raised before the startup form is created. You can handle that event and test for the existence of your file. If the file doesn't exist then you tell the application not to create a startup form and it will simply exit gracefully. Select your project in the Solution Explorer and press the Properties button at the top to open the project properties. Go to the Application tab and press the View Application Events button. You now create a handler for the Startup event as normal. Select (MyApplication Events from the top-left drop-down list and Startup from the top-right. You now add code like the following:
VB Code:
Private Sub MyApplication_Startup(ByVal sender As Object, _
ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
e.Cancel = Not IO.File.Exists("file path here")
End Sub
If the file doesn't exist e.Cancel is set to True, which cancels the application startup, i.e. prevents a startup form being created, thus the app simply exits of its own accord.
Environment.Exit is usually used with commandline apps that may have to return an exit code to the process that spawned them. Application.Exit should be used for WinForms apps unless you specifically have to return an exit code, which would be rare. Application.Exit will work but you must keep in mind that the app doesn't just disappear when you call it. Execution must work its way back down the method call stack until it reaches a point where the system can initiate the shutdown. If you want to use Application.Exit then you should make sure that there is no more user code to be executed after the call.
-
Sep 21st, 2006, 09:33 PM
#5
Thread Starter
Junior Member
Re: Exiting Program
Ok, I'm going to use System.Environment.Exit(0) but its still good to know the Startup way. Thanks so much for all your help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|