?
Solved

VB.NET --- Exit Program on Error

Posted on 2007-08-06
5
Medium Priority
?
627 Views
Last Modified: 2012-06-27
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
Comment
Question by:cfg1980
  • 2
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 19641298
Application.Exit vs. Environment.Exit - pro's and contra's
http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

Application.ExitThread
0
 

Author Comment

by:cfg1980
ID: 19641405
System.Windows.Forms.Application.Exit() worked for me.  Thank you
0
 

Author Comment

by:cfg1980
ID: 19641412
Opps sorry, I meant System.Environment.Exit(-1) worked for me....not the Application.Exit()
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 19641468
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 19641639
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question