1,105,402 Community Members

Code for Exiting a C# Application

Member Avatar
Ghost
Posting Whiz
354 posts since Aug 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
1
 

Hi,

i'm new 2 C#, so my question may seem stupid... but, what is the code to exit an application.

I know in java it is:

System.exit(0);

Thanx in advanced,
C++

Member Avatar
r0ckbaer
Junior Poster in Training
55 posts since Dec 2003
Reputation Points: 3 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
1
 

Console => Environment.Exit(0)
Forms => Form.Close()

Member Avatar
crypter
Light Poster
34 posts since Jul 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

r0ckbear for the forms if you put Form.Close(); you will only close the form without actually exiting the application, what i put is
Application.Exit();

Member Avatar
Ghost
Posting Whiz
354 posts since Aug 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

thank u very much

Member Avatar
r0ckbaer
Junior Poster in Training
55 posts since Dec 2003
Reputation Points: 3 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

r0ckbear for the forms if you put Form.Close(); you will only close the form without actually exiting the application, what i put is
Application.Exit();

Application.Exit() is indeed the method to use when you got several forms open, but when your app consists of only one MainForm and you close it, it will exit the application as well.

Member Avatar
SethWebster
Newbie Poster
7 posts since Apr 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

rOckbaer,

Semantically, you are incorrect, even though in most cases that would work. What you stated will exit the Application Loop that that Application represents. For multiple Application Loops, the must all be terminated.

Member Avatar
Iron_Cross
Junior Poster
117 posts since Jul 2003
Reputation Points: 32 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 5 [?]
 
0
 

If you're application consists of of a main form (i.e. one created by using Application.Run(new Form()); ) then closing that form exits the application. Although, if you're doing it programaticly, then I wouldn't use that.

serkan sendur
Postaholic
2,057 posts since Jan 2008
Reputation Points: 821 [?]
Q&As Helped to Solve: 129 [?]
Skill Endorsements: 1 [?]
Banned
 
0
 

i cant exit the application using Application.Exit()

Member Avatar
tunnweaung
Newbie Poster
1 post since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i cant exit the application using Application.Exit()

how abt Application.ExitThread()?

Member Avatar
inderneel
Newbie Poster
1 post since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Can anybody please tell how to exit from an application?
Application.Exit() and
Application.ExitThread() both are not working.

I am calling this new form from another form using
frm.ShowDialog()

Please help..

Member Avatar
sknake
Senior Poster
3,957 posts since Feb 2009
Reputation Points: 1,620 [?]
Q&As Helped to Solve: 747 [?]
Skill Endorsements: 25 [?]
Featured
 
1
 

Please start new threads for your questions.

You probably have another thread created that is not running as a background thread which will stop your application from closing until the thread aborts. You probably need to review if this is this case, or upload your project demonstrating the behavior.

Please do not respond to this thread -- create a new one.

Member Avatar
forisrea
Newbie Poster
1 post since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

System.Environment.Exit(0) to exit from c#

Member Avatar
puppykillaz
Newbie Poster
24 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
Environment.Exit(0);
Member Avatar
arunkumars
Posting Whiz in Training
204 posts since Jul 2009
Reputation Points: 16 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 0 [?]
 
0
 

well,
a.If there is only one form in that application you are using, then Application.Exit will close the application itself.

b.If there is one more Form that you are navigating to from the main form, then in the new form, use Form2.Close(), this will take you to the back to the main application that you are using.

c. If you want to close both of them, then first give Form2.Close() and then give Application.Exit(). That would suffice.

Mark it solved, if its working fine.

Member Avatar
iconoclazt
Newbie Poster
14 posts since Dec 2010
Reputation Points: 11 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

i'm new 2 C#, so my question may seem stupid... but, what is the code to exit an application.

I know in java it is:

System.exit(0);

Thanx in advanced,
C++

System.Environment.Exit(0);
Member Avatar
ShahanDev
Junior Poster
193 posts since Dec 2010
Reputation Points: 41 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 3 [?]
 
0
 

@inderneel

Lets say you have two forms: Form1 and Form2.

You called Form2 from Form1 like this:
Form2 frm=new Form2();
frm.ShowDialog();

My question is are you willing to exit the Application from Form2 ???

Try:

Environment.Exit(0);

The exit method terminates the PROCESS and your form(s) lies under the same process :)

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

Friends!

Please do not resurrect threads that are years old. By doing so you run the risk of confusing current posters. If you have any questions please ask. You are welcome to start your own threads. Have a look at forum rules.

Thread Locked.

Nat2012
Newbie Poster
1 post since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

The best method to use is Environment.Exit(int returnCode).

This helps you even to return a code to the OS.

This guy has a good brief explanation to it:

http://www.ilearnttoday.com/tag/environment-exit0

Natalie

Member Avatar
Michael27
Junior Poster
106 posts since Jul 2010
Reputation Points: 29 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 6 [?]
 
0
 

Don't forget about using this
http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx

and how was this thread locked when a newbie can post in it?

Member Avatar
vedegadegosi
Newbie Poster
1 post since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Environment.Exit(0); works fine, but is there another salution?
this.Close() and also this.Dispose() causes an exception.

You
Post:
Start New Discussion
Tags Related to this Article