What is the another way to Close the application other than Application.Exit()
What is the another way to close the application in C#, wit开发者_StackOverflowhout using the Application.Exit()
In a console app you can use Environment.Exit(0)
. The 0
parameter indicates this application ended with no errors. Obviously, if you're just exiting normally, you should probably just code so that normal program flow exits the Main
method.
In Winforms, just close all forms using the Form.Close()
method.
in winforms and wpf you can use this.close();
In console applications, just return
your path through back and from Main
.
Environment.Exit
would disrupt the program's flow control in potentially destructive manner and requires higher security settings than necessary. Imagine your customer is tech savvy, then asks you why your app needs this and that security flag, and you have to answer "Because we use Environment.Exit
instead of return." Surely his next answer would be "Why?". Do you know an answer that he/she couldn't counter with "Can't you use return
? If you have an unrecoverable exception, why don't you just not catch
it?"
Maybe Environment.Exit()
has its place, but I didn't need it a single time in some years of C#.
For Winforms, use Form.Close()
in your main-window, which will send a clean Close
-event to the event-loop, without circumventing normal flow.
(sidenote: if you ever happen to use C++, calling exit()
is almost always incorrect as it circumvents the whole RAII/destructor thing)
I got the solution for my question. Below code will solve my query,
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process.GetCurrentProcess().Kill();
}
}
}
You can call Form.Close()
on the primary Form
(the one that was passed in through Application.Run(Form)
).
Environment.Exit() //Console Application
Form.Close() //WinForms Application
You can try checking this http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx
and
http://discuss.joelonsoftware.com/default.asp?dotnet.12.402651.5
精彩评论