Nothing shutsdown my application!

  • Thread starter Thread starter Pat Reddy
  • Start date Start date
P

Pat Reddy

I have a relatively simple single threaded application written in
VB.NET. When I close the application I loop through all open forms and
close them, like this basically

dim f as Form
for each f in forms
f.close()
Next

I heard this was better than using Application.Exit.

But, regardless of which I use, if I try to exit this way from a module,
like my DataAccess module upon an error connecting to a database for
example, all the forms close but the code that called the database
connection sub in my module continues to run. Here is some code...

Report.vb is a class containing a sub called UpdateDatabase that simply
calls a function in my DataAccess module:
-----------------------
Public Sub UpdateDatabase()
DataAccess.UploadTagsToDatabase(Me)
End Sub
-----------------------

If an exception is thrown in the DataAccess module I close all the
forms, or call Application.Exit. Either way, the forms all close but
the code returns to the calling sub in my Report.vb class. When it then
attempts to return to the code that instantiated the Report object and
called UpdateDatabase I get a null object exception because the form no
longer exists! Help, please, why did the application not die when I
told it to?

Thanks.

Pat Reddy
Industrial Controls Engineer
MAVERICK Technologies
 
When you say "module", do you mean in the VB sense? That can be treated as a
static class.

It is good you are not using Application.Exit but why are you iterating thru
all the forms and closing them? All you have to do is close the main form
(i.e. the one that you passes to Application.Run).

If you have object A calling object B which calls object C, you shouldn't
expect to do something in C which will then exit your app without unwinding
the call stack. Instead, return to B some flag which returns to A and there
you are at the top of the stack (e.g. in a button_click event handler or
whatever) so you can close your main form.

Cheers
Daniel
 
Back
Top