• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 640
  • Last Modified:

VB.NET --- Exit Program on Error

I'm attempting to write a VB.Net program where if any of my code returns an error, the program closes.  Here's what I'm trying, but its not working.  I've tried both Application.Exit() and Me.Close().  Neither seems to work, the program just continues on.  Can someone please tell me what I'm doing wrong?


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  'call the main funtion
  theFunction()

End Sub


Private Sub theFunction

 Try
   here's where my code goes
 Catch
   'there was an error, need to quit the program
   QuitApp()
 End Try

End Sub


Private Sub QuitApp()

 here's where my code goes to close all open objects

 Application.Exit()

End Sub
0
cfg1980
Asked:
cfg1980
  • 2
  • 2
1 Solution
 
DhaestCommented:
Application.Exit vs. Environment.Exit - pro's and contra's
http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

Application.ExitThread
0
 
cfg1980Author Commented:
System.Windows.Forms.Application.Exit() worked for me.  Thank you
0
 
cfg1980Author Commented:
Opps sorry, I meant System.Environment.Exit(-1) worked for me....not the Application.Exit()
0
 
Fernando SotoRetiredCommented:
Hi cfg1980;

The default Shutdown mode for a Windows Form application is "When startup form is closed." In .Net Framework 2.0 and before this can be changed to "When last form closes." In .Net 3.0 they added a third option which is "On Explicit Shutdown" which the application only shuts down when Application.Shutdown() or Application.Shutdown(int32) where the int32 is the return code is called.

The class that has the function "Private Sub theFunction" if it is in the form that the program starts on then this should work, Me.Close(), If not then you need a reference to the main form so that you can close it with the Me.Close() method.

Fernando
0
 
Fernando SotoRetiredCommented:
Hi cfg1980;

A couple of issues with using System.Environment.Exit(-1). The Form.Closing or Form.Closed methods are not called and so any cleanup of all open forms need to be done individually. The other thing is that according to Microsoft System.Environment.Exit(-1) can cause a SecurityException - if "The caller does not have sufficient security permission to perform this function. " so check out the code.

Fernando
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now