By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,769 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,769 IT Pros & Developers. It's quick & easy.

Code to quit an application

P: n/a
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?
Jan 24 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Jay" <-wrote in message news:%2****************@TK2MSFTNGP03.phx.gbl...
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.
>

Jan 25 '07 #2

P: n/a
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.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:uC**************@TK2MSFTNGP03.phx.gbl...
>
"Jay" <-wrote in message news:%2****************@TK2MSFTNGP03.phx.gbl...
>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.
>>


Jan 25 '07 #3

P: n/a
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);
}

On Jan 25, 2:14 pm, "Dave Sexton" <dave@jwa[remove.this]online.com>
wrote:
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.

--
Dave Sextonhttp://davesexton.com/bloghttp://www.codeplex.com/DocProject(Sandcastle in VS IDE)

"Ben Voigt" <r...@nospam.nospamwrote in messagenews:uC**************@TK2MSFTNGP03.phx.gbl. ..
"Jay" <-wrote in messagenews:%2****************@TK2MSFTNGP03.phx.gb l...
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.
Jan 25 '07 #4

P: n/a
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 news:%2****************@TK2MSFTNGP03.phx.gbl...
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?

Jan 25 '07 #5

P: n/a
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.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Jay" <-wrote in message news:uZ**************@TK2MSFTNGP03.phx.gbl...
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 news:%2****************@TK2MSFTNGP03.phx.gbl...
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?

Jan 25 '07 #6

P: n/a
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(fals e);
//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(fals e);
//validation logic
...
if(validatesOk) Application.Run(new FormMain());
}

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:O6**************@TK2MSFTNGP05.phx.gbl...
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.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Jay" <-wrote in message news:uZ**************@TK2MSFTNGP03.phx.gbl...
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 news:%2****************@TK2MSFTNGP03.phx.gbl...
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?
Jan 25 '07 #7

P: n/a
Hi Jay,

You got it :)

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Jay" <-wrote in message news:es**************@TK2MSFTNGP03.phx.gbl...
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(fals e);
//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(fals e);
//validation logic
...
if(validatesOk) Application.Run(new FormMain());
}

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:O6**************@TK2MSFTNGP05.phx.gbl...
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.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Jay" <-wrote in message news:uZ**************@TK2MSFTNGP03.phx.gbl...
>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
news:%2****************@TK2MSFTNGP03.phx.gbl...
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?

Jan 25 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.