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++

Re: Code for Exiting a C# Application 80 80

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

Re: Code for Exiting a C# Application 80 80

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();

Re: Code for Exiting a C# Application 80 80

thank u very much

Re: Code for Exiting a C# Application 80 80

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.

Re: Code for Exiting a C# Application 80 80

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.

Re: Code for Exiting a C# Application 80 80

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.

Re: Code for Exiting a C# Application 80 80

i cant exit the application using Application.Exit()

Re: Code for Exiting a C# Application 80 80

i cant exit the application using Application.Exit()

how abt Application.ExitThread()?

Re: Code for Exiting a C# Application 80 80

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..

Re: Code for Exiting a C# Application 80 80

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.

Re: Code for Exiting a C# Application 80 80

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

Re: Code for Exiting a C# Application 80 80
Environment.Exit(0);
Re: Code for Exiting a C# Application 80 80

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.

Re: Code for Exiting a C# Application 80 80

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);
Re: Code for Exiting a C# Application 80 80

@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 :)

Re: Code for Exiting a C# Application 80 80

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.

Re: Code for Exiting a C# Application 80 80

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

Re: Code for Exiting a C# Application 80 80

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?

Re: Code for Exiting a C# Application 80 80

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

Re: Code for Exiting a C# Application 80 80

this.Close() and also this.Dispose() causes an exception.

Then I'd wager you have a problem in your cleanup code. Environment.Exit() probably works because it terminates the process in an unclean way for form applications. I'd further speculate that Application.Exit() fails as well, because it cleanly ends the application.

In other words, your code is broken. Trace the exception and find out where it's coming from, then fix the problem. The closing issue will be resolved as well.

Re: Code for Exiting a C# Application 80 80

Environment.Exit(0);
Thos statement not working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.