Click here to Skip to main content
15,886,812 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am using a windows forms application. frmMain is the application startup object. In Sub New, I check if a config file is present, and if not, I want the form to close, and end the application. If the config file is present, I need to get a path to a database from the config file, and check that the database file exists. If the database file doesn't exist, the application must close.
With the code below, I get a message that the config file is not present, but then I still get an exception "Cannot Access a Disposed Object - frmMain" after the messagebox is closed.
I tried using Application.Exit instead of Me.Close, but then the frmMain still opens after the messagebox in Sub New is displayed.
I understand that End should not be used since this does not dispose of all objects correctly.

How do I quit the application cleanly if there is a problem with either the config file or the database file?

VB
Public Sub New()

       ' This call is required by the designer.
       InitializeComponent()

       ' Add any initialization after the InitializeComponent() call.
       If Not IO.File.Exists(Application.StartupPath & "\simmstock.config") Then
           MessageBox.Show("Config file is missing. The application cannot start")
           Me.Close()
       Else
           GetConfigData()
           LoadAllTables()
           Setup()
           SetupUser()
           Loading = False
       End If

   End Sub

   Private Sub GetConfigData()
       Xml = New XMLManager("Simmstock.config")
       dbPath = Xml.GetKey("DBPath")
       If IsNothing(dbPath) Then
           MessageBox.Show("Error in config file. Confirm DBPath Key. Application cannot start.")
           Me.Close()
       End If
       If Not IO.File.Exists(dbPath) Then
           MessageBox.Show("Cannot access the Database specified in the config file. The application cannot start. Ensure you have access to the network or call your System Admisistrator")
           Me.Close()
       End If
       DA = New DataAccess(dbPath, 2007)
       UserID = Xml.GetKey("User")
   End Sub
Posted
Updated 20-Jul-18 21:05pm
Comments
Sergey Alexandrovich Kryukov 28-Jan-12 2:35am    
What is "End"?! End is the keyword, has nothing to do with application termination.
--SA
Richard.Berry100 28-Jan-12 2:54am    
I'm not an expert, but if I put 'End' in place of Me.Close, it does kill the application?

If I type End, intellisense shows:
End Statement
Stops Execution Immediately

The problem is where you are doing the test.
What you are doing is trying to kill the form in the constructor - which doesn't quite work because the constructor returns and the framework then tries to open the killed and disposed form.

Move your code to the Load event for the form, and use the Close method - it will work fine then.
 
Share this answer
 
First, please see my comment to the question. Functionally, there is no such thing as "End".

You've messed up some simple things in your head. The method Close is actually System.Windows.Forms.Form.Close. It closes the form, which has nothing to do with application. But! — let's take a look at the entry point:

Sub Main()
    '...
    Dim myMainForm As New SomeFormClass()
    'by this call, it is decided which form is main:
    Application.Run(myMainForm) 
End Sub


The main form is different in one aspect: if it closes, the method Application.Run exits. That's it. That's why the application ultimately terminates, as you can see from the code above.

The method Application.Exit just exits the application unconditionally; doing some preliminary pre-exit actions, such as disposing of all forms and, hence, all composed objects as required.

Now, the problem is: if the form is not main, you can close it, but you cannot open it again, otherwise you will get the exception you mentioned. So the pattern should be this: if you want to allow the user to hit the close button but want to use this form again later… capture the attempt to close it and hide it instead. This is done by handling the event FormClosed or overriding the virtual method OnFormClosing, please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing.aspx[^].

In this articles, pay attention on the usage of the property Cancel of the event arguments parameter. You will need to assign true to it to prevent default action, that is, the closing.

—SA
 
Share this answer
 
v2
Comments
Richard.Berry100 28-Jan-12 3:14am    
Thanks for the info. I set the frmMain as my Startup Form under Project Properties, so I'm not really using Sub Main() as your example above? Or maybe that is what Visual Studio is doing? It seems to work fine if I put the file validation in the Form Load event though. Not sure if there is a difference?
Sergey Alexandrovich Kryukov 29-Jan-12 0:20am    
You did not understand. You certainly use Sub Main in your code; this is the code written for your when you create a Forms application from template. This is your entry. I just explained how application is closed in different situation, because it was your concern.

Please look again. This is what you really need to understand on this matter, your functionality is the derived from these facts.
--SA
Sergey Alexandrovich Kryukov 29-Jan-12 0:24am    
Also, you should not think of your application as something that Visual Studio is "doing", even partially. There is no such thing. It helps you to generate part of routine code, nothing else. You can develop the code, uninstall the Studio and still be able to build the application and run it, review its code, etc.

It does not even include compiler and builder for project and solution files -- all is done by compilers and MSBuild which are part of redistributable .NET Framework.

All the code is yours. You are the only author and supposed to know where is your "Main" and everything else.
--SA
Richard.Berry100 29-Jan-12 1:08am    
Thanks SA. I did not realize the difference between 'Application' and my MainForm, and that the Application is opening the form from Sub Main. Is there a way to see the Sub Main in Visual Studio?

Also in my case, what I actually wanted to do was close the form properly, which would then cause the Application.Run to exit. (Not Application.Exit)
From the link you suggested, if the form is closed properly: The FormClosing event occurs as the form is being closed. When a form is closed, it is disposed, releasing all resources associated with the form.
I am largely 'self/internet' taught, and have clearly missed out on a lot of the basics, so thanks for your time to explain!
Sergey Alexandrovich Kryukov 29-Jan-12 3:16am    
There is nothing is your project you cannot see.

I don't use VB.NET, but auto-generated file with the entry point method (Main, and it can be either function or Sub, parameters are optional, too) is probably "program.vb". If not, search for "Application.Run" (it should be only on one place). You don't have anything to do with this file (in a typical "usual" application), I just needed to explain where the application is started and ended and what defines which form is main.

Entry point of the application is one of the fundamental things every one should know. You can trust yourself as a developer only if you know how to write a whole applications with your own hands without all those stupid Designers and project templates. Trust me, this is not difficult at all. Auto-generated code is provided for you to safe you from some boring typing, but not because you are not supposed to know anything. You should understand every line in your code, otherwise the whole idea of programming in in vain.

As to the self-taught, by the way: do you think other people are very different? Do you think somebody teaches such a small thing like one particular language or framework in college? Even if they do, it makes no sense. College should teach mathematics, say, general theory of languages and a lot more, but not a concrete language; this is just a waste of valuable time. I simply don't know decent people who are not self-taught. I mean, such things cannot be an excuse.
--SA
VB
End
this skips the Closing argument so it goes right ahead and closes the whole aplication.
VB
Me.Close
this just closes one form and goes through the closing argument.
VB
Application.Exit
this closes the whole application but goes through the closing argument for each and every form.

Hope this helps!
 
Share this answer
 
Comments
Richard Deeming 23-Jul-18 13:52pm    
Asked, answered, and solved SIX AND A HALF YEARS AGO.

Stick to answering recent questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900