Home All Groups Group Topic Archive Search About

Confused about Application.Exit

Author
28 Feb 2006 2:56 PM
William Sullivan
Why will the following code snippet run forever?

While(true)
{
    if(MessageBox.Show("Exit now?","Close?") == DialogResult.OK)
    {
         Application.Exit();
    }
}

How do you exit an application from within a While loop?

Author
28 Feb 2006 3:05 PM
Nick Hounsome
"William Sullivan" <WilliamSulli***@discussions.microsoft.com> wrote in
message news:7EDDC9E2-BA82-4B48-91FE-3A089179CB9B@microsoft.com...
> Why will the following code snippet run forever?
>
> While(true)
> {
>    if(MessageBox.Show("Exit now?","Close?") == DialogResult.OK)
>    {
>         Application.Exit();
>    }
> }
>
> How do you exit an application from within a While loop?

You don't - you have to return to the message pump so that it can return
from the Run() method.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsapplicationclassexittopic.asp

I don't think that there is an equivalent of the C function exit().
Author
28 Feb 2006 3:31 PM
William Sullivan
Foolish me.  Just after I posted, I realized I was trying to use
Application.Exit in the constructor of an object I was creating before
calling Application.Run().  Instead, I just threw an exception and caught it
in Main() so I could then return prior to running the pump.  Thanks.

Show quote
"Nick Hounsome" wrote:

>
> "William Sullivan" <WilliamSulli***@discussions.microsoft.com> wrote in
> message news:7EDDC9E2-BA82-4B48-91FE-3A089179CB9B@microsoft.com...
> > Why will the following code snippet run forever?
> >
> > While(true)
> > {
> >    if(MessageBox.Show("Exit now?","Close?") == DialogResult.OK)
> >    {
> >         Application.Exit();
> >    }
> > }
> >
> > How do you exit an application from within a While loop?
>
> You don't - you have to return to the message pump so that it can return
> from the Run() method.
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsapplicationclassexittopic.asp
>
> I don't think that there is an equivalent of the C function exit().
>
>
>
Author
28 Feb 2006 6:46 PM
José_Manuel_Agüero
Hello Nick,

You can substitute the C function exit() with System.Environment.Exit():
http://msdn.microsoft.com/library/?url=/library/en-us/cpref/html/frlrfSystemEnvironmentClassExitTopic.asp?frame=true

Regards.


Show quote
"Nick Hounsome" <nh***@nickhounsome.me.uk> escribió en el mensaje news:LEZMf.62912$Q22.21967@fe1.news.blueyonder.co.uk...
|
| "William Sullivan" <WilliamSulli***@discussions.microsoft.com> wrote in
| message news:7EDDC9E2-BA82-4B48-91FE-3A089179CB9B@microsoft.com...
| > Why will the following code snippet run forever?
| >
| > While(true)
| > {
| >    if(MessageBox.Show("Exit now?","Close?") == DialogResult.OK)
| >    {
| >         Application.Exit();
| >    }
| > }
| >
| > How do you exit an application from within a While loop?
|
| You don't - you have to return to the message pump so that it can return
| from the Run() method.
|
| http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsapplicationclassexittopic.asp
|
| I don't think that there is an equivalent of the C function exit().
|
|

AddThis Social Bookmark Button