Confused about Application.Exit

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
William Sullivan said:
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/d...stemwindowsformsapplicationclassexittopic.asp

I don't think that there is an equivalent of the C function exit().
 
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.
 
Hello Nick,

You can substitute the C function exit() with System.Environment.Exit():
http://msdn.microsoft.com/library/?...ystemEnvironmentClassExitTopic.asp?frame=true

Regards.


"Nick Hounsome" <[email protected]> escribió en el mensaje |
| message | > 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/d...stemwindowsformsapplicationclassexittopic.asp
|
| I don't think that there is an equivalent of the C function exit().
|
|
 
Back
Top