Friday, July 14, 2006 11:39 AM Christopher

What's this? My Application won't Exit?

There are a few things that have to happen for this "regression" to show up. First, you have to be working in .net 2.0 WinForms. Second, you must be using a custom application context. Third and most importantly, you must have at least one form open in your application that handles its closing event and sets CancelEventArgs.Cancel = true.

What's the behavior? While operating under a custom ApplicationContext, what would be my Main form is closed by the user. Since the application context is not associated with that form, I decide to call "Application.Exit()" in an override of OnClosing. My Main form closes, but any hidden forms stick around, thus leaving the application running until you open up task manager (or something similar) and manually kill the process.

Why does it happen? WinForms 2.0 introduced an overload to Application.Exit that accepts a CancelEventArgs parameter. This is what is called by Application.Exit(). If even one of your open forms doesn't think it needs to close itself for whatever reason and sets its CancelEventArgs to true, Application.Exit stops processing and returns control to the application.

Why did I call it a "regression"? This behavior has changed between .net 1.1 and 2.0. In 1.1, if I call Application.Exit, by golly the application exits! I understand the reasoning behind the change. I just wish that there was some indication to it other than Reflector. A comment for intellisense perhaps? Something.

Will it matter to most WinForm projects? Probably not. If you use the default application context (by calling Application.Run(new MainForm()), which is what VS.NET spits out for you from the template) you do not have the same problem. Closing the main form exits the application as expected. If you're using a custom ApplicationContext, tho, watch out.

Filed under: ,

Comments

# re: What's this? My Application won't Exit?

Monday, October 15, 2007 1:58 PM by Seshu

HI,

I have a Windows Application developed using VB.net 2.0. Initially it was an EXE and later we converted it to a DLL. So that it can reside on GAC.. (i.e a windows forms application as a DLL)

My doubt is I have condition in Form_Load handler,

If UserType="L" Then

  Me.Close()

End If

Control is hitting Me.Close() logic but after that it is continuing executing other handlers.. and cauing some unknown errors.. How to solve this? How to stop the application once it hits Me.Close() ?

Thanks

netpicker9@yahoo.com

# re: What's this? My Application won't Exit?

Wednesday, July 02, 2008 10:18 AM by Srimoyee

Use Environment.Exit(0)

# re: What's this? My Application won't Exit?

Monday, October 06, 2008 11:21 AM by Bruce

# re: What's this? My Application won't Exit?

Wednesday, July 02, 2008 10:18 AM by Srimoyee

Use Environment.Exit(0)

Srimoyee. Exactly what I was Looking for!!!!

Very simple line of code to fix a big problem :))

# re: What's this? My Application won't Exit?

Tuesday, January 06, 2009 2:18 AM by varun

Use Environment.Exit(0)

It worked for me toooooooo.. thanx man.. :-)

# re: What's this? My Application won't Exit?

Friday, January 16, 2009 8:24 PM by Name (required)

Just make sure you aren't writing to a file or database when making the Environment.Exit(0) call, or you will corrupt the file.

# re: What's this? My Application won't Exit?

Wednesday, October 21, 2009 7:03 AM by user

Or just use the FormClosing event handler and set e.Cancel = false; You could also do a check to see why e.Cancel is equal to true in the first place and handle it appropriately.

# re: What's this? My Application won't Exit?

Saturday, September 03, 2011 3:55 AM by Pupeevetlylit

Доброго времени суток,  

Хочу представить вам новоявленный лабаз курительных смесей

сайт магазина http://spice-family.ru  

3г микса Relax - 1,500 р. + доставка (ems, pony get across)  

По вопросам опта чертить поурочно в скайп - FomaX2

# re: What's this? My Application won't Exit?

Saturday, September 03, 2011 4:00 AM by Pupeevetlylit

Доброго времени суток,  

Хочу представить вам новый лабаз курительных смесей

сайт магазина http://spice-family.ru  

3г микса Rest - 1,500 р. + доставка (ems, pony set)  

Сообразно вопросам опта отмечать вразброд в скайп - FomaX2

# re: What's this? My Application won't Exit?

Friday, December 23, 2011 2:16 PM by puntygale

look at   to take huge discount   to get new coupon

Leave a Comment

(required) 
(required) 
(optional)
(required)