Code to quit an application

J

Jay

In my C# code, I'm attempting to display a message box then quit the win form application I'm
writing if a certain type of error occurs when the application starts up.

In the main form's constructor - FormMain() - if this error occurs, I display a message box then
call Application.Exit().

The problem is that I also need to have some code in my FormMain_FormClosing event handler, but this
prevents Application.Exit() from quitting the application. Is there another way of quiting an
application that doesn't call FormMain_FormClosing?
 
B

Ben Voigt

Jay said:
In my C# code, I'm attempting to display a message box then quit the win
form application I'm
writing if a certain type of error occurs when the application starts up.

In the main form's constructor - FormMain() - if this error occurs, I
display a message box then
call Application.Exit().

The problem is that I also need to have some code in my
FormMain_FormClosing event handler, but this
prevents Application.Exit() from quitting the application. Is there
another way of quiting an
application that doesn't call FormMain_FormClosing?

If it's an eventhandler, you can unsubscribe it before exiting. You can
also set a variable, and let OnFormClosing check the variable and return
without calling the base classes (that calls event handlers) when you want a
fast exit.
 
D

Dave Sexton

Hi,

An alternative is to use the Form.Closing event instead of Form.FormClosing.
The Closing event doesn't get raised when Application.Exit is called, but
the FormClosing event does.
 
N

nick.fletcher

One thing you can do is listen for the application closing windows
messages and disconnect any event handlers if a form close is detected

eg.
private const int WM_CLOSE = 0x0010;
private const int WM_QUERYENDSESSION = 0x0011;
private const int WM_ENDSESSION = 0x0016;

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CLOSE || m.Msg == WM_QUERYENDSESSION
|| m.Msg == WM_ENDSESSION)
{
//disconnet event handlers
}
base.WndProc(ref m);
}
 
J

Jay

Thanks for your replies everyone. I'll set a variable as Ben suggested.

Now I have another problem. Application.Exit() appear to do nothing when it's called anywhere in the
main form's constructor - FormMain(). The programme contines to run as if it wasn't called, and
stepping through with the debugger shows that it does nothing too. It appears to work anywere else,
however. Do you have any suggestions or alternatives, since the error checking is performed in the
constructor, and I must quit the application if the error occurs.



"Jay" <-> wrote in message In my C# code, I'm attempting to display a message box then quit the win form application I'm
writing if a certain type of error occurs when the application starts up.

In the main form's constructor - FormMain() - if this error occurs, I display a message box then
call Application.Exit().

The problem is that I also need to have some code in my FormMain_FormClosing event handler, but this
prevents Application.Exit() from quitting the application. Is there another way of quiting an
application that doesn't call FormMain_FormClosing?
 
D

Dave Sexton

Hi,

You shouldn't be performing the validation logic and throwing an exception
in the constructor of your Form unless you're simply validating arguments.
Do all of the appropriate argument and non-argument validation before
constructing an instance of the Form in the calling code, and decide then
whether you even need to construct the Form in the first place. If it's the
main form of your application then you won't even need to call
Application.Exit - just let the thread exit on its own.
 
J

Jay

Thanks for your help Dave.

Do you mean I should put the validation in main()? If so, is between Application.Set and
Application.Run a sensible place (see below)?

static void Main(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//validation logic
...
Application.Run(new FormMain());
}


"If it's the main form of your application then you won't even need to call Application.Exit - just
let the thread exit on its own."

It is the main form of my application. I'm not sure what you mean by "just let the thread exit on
its own". Do you mean I should use something like:

static void Main(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//validation logic
...
if(validatesOk) Application.Run(new FormMain());
}



Hi,

You shouldn't be performing the validation logic and throwing an exception
in the constructor of your Form unless you're simply validating arguments.
Do all of the appropriate argument and non-argument validation before
constructing an instance of the Form in the calling code, and decide then
whether you even need to construct the Form in the first place. If it's the
main form of your application then you won't even need to call
Application.Exit - just let the thread exit on its own.
 

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