Error and close application

R

Rodrigo Juarez

Hi
I'm using Visual Studio 2005 with visual basic, developing winforms
applications
I'm adding try catch blocks for error handling and I want to close the
application when I got an error.
Closing the form is not enough because there is a lot of forms open

TIA

Rodrigo Juarez
 
A

Armin Zingler

Rodrigo Juarez said:
Hi
I'm using Visual Studio 2005 with visual basic, developing winforms
applications
I'm adding try catch blocks for error handling and I want to close
the application when I got an error.
Closing the form is not enough because there is a lot of forms open

If you don't have multiple forms: Application.ExitThread


Armin
 
R

RobinS

Rodrigo Juarez said:
Hi
I'm using Visual Studio 2005 with visual basic, developing winforms
applications
I'm adding try catch blocks for error handling and I want to close the
application when I got an error.
Closing the form is not enough because there is a lot of forms open

TIA

Rodrigo Juarez

So whenever you get an error, you want to close all of the forms and exit
the application?

Is that what you're trying to achieve?

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 
R

RobinS

If you are catching the exceptions in your code, you can choose whether to
throw them up another level or handle them. Generally, it's best to handle
them "in place". Why would you want to shut everything down just because
there is an error?

At any rate, if you want to do this, you have to figure out where you want
to close your forms. If you do it in a Form_Closing event, you'll need to
close all *other* forms. If you try to close a form in its own Form_Closing
event, it will probably loop infinitely or something.

For Each myForm As Form In My.Application.OpenForms
If myForm.name <> "whateverformiscallingthis" Then
myForm.Close()
End If
Next

If you are letting the exceptions bubble up to the top and handling them in
the Application UnhandledException event that I told you about in the other
thread, you can just close all the forms there.

For Each myForm As Form In My.Application.OpenForms
myForm.Close()
Next

IMHO, this is not a best practice. <:-O

Robin S.
-----------------------------------------
 
J

Jeffrey Tan[MSFT]

Hi Robin ,

Thank you for sharing your idea with Rodrigo!

I agree with most of your comment. Based on my experience, there are 2
types of exceptions/errors in application, one is expected and one is
unexpected. Expected error(such as file not found etc..) is known to the
developers, so it would be safe to gracefully recover from this error and
let the application continue to run without closing. In the unexpected
error scenario, I think it may be better to close the application, since
developers know nothing of this error, it may be hardware error, memory
insufficiency or even memory corruption, so it may be unsafe to run from
this situation. The recommended solution in this scenario may be closing
the entire application. Also, this is default behavior of Win32 unhandled
exception filter in NTDLL.

Additionally, Rodrigo, you may also use Application.Exit() method to close
the entire applicatin if you think it is suitable. This will correctly
inform all message pumps that they must terminate, and then close all
application windows after the messages have been processed based on the
MSDN. So I assume this method may meet your need.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top