Exiting an application

  • Thread starter Thread starter Andrew Warren
  • Start date Start date
A

Andrew Warren

I'm trying to exit a Windows Forms application while in
the form's constructor (after InitializeComponent() has
been called) and am finding that calling Application.Exit
() still leaves the form displayed and running. Trying
to force the form to close with this.Close() does not
work either.

Any suggestions?

Thanks.
 
It sounds like you are doing some sort of check in the constructor and
aborting the app if it fails. If this is the case I can think of a couple
of ways to avoid the result you are experencing:

1) Preform the check in your main function before calling Application.Run().
Something like:

static void Main() // Not tested
{
...
if(<test_pass>)
{
Application.Run(new App());
}
else
{
// run any error code here, for example:
Application.Run(new ErrorMsgBox());
}
}

2) Set the form's visible property to false and preform you check using the
form's Load event.
Note: Due to a bug in the .NET framework your form will still flash on the
screen for half a second before closing on XP systems running the 1.0
version of the framework.

Hope this helps.

Dave
 
Hello Ann!

Just as an FYI to everyone, Close methods should call dispose internally*
(so there shouldn't be a need to call them separately), and you shouldn't
call members of a class after calling the Dispose method.
Please don't take this as criticism against your post, I always try to
encourage everyone to participate and contribute :-)

-Rob Teixeira [MVP]

* if you look at the IL for the Form class, Close doesn't call Dispose
directly, it posts a close message to form, and when the form handles
WM_CLOSE, it calls Dispose.
 
These work: Probably pretty much in order of my preferred methodology:

1) Separate the form creation and Application Run method and check a bool
variable, such as this:
private bool doExit = false;
...
static void Main()
{
Form1 f1 = new Form1(); // set doExit to true in constructor if
there's a problem.
if( !f1.doExit )
{
Application.Run( f1 );
}
}
...
2) Can you delay the exit until the Load event? You can't close an
Application until after the message pump has begun processing, which happens
after your main Form constructor.

3) Throw an exception in your constructor and catch it in Main()

Chris R.
 
I think you are right. :)

Thank you so much for correcting me. I really do appreciate it! :)


Ann
 
Thanks Chris. I'd already coded a solution along the
lines of your first option but I couldn't understand why
Application.Exit() _didn't_! Your comment in suggestion
2 answers this to some extent but raises the question -
when does the message pump start processing exactly? From
your comments it's some time between exiting the
constructor and exiting form.Load().
 
Thanks Dave. I rearranged the code to operate along the
lines of your first suggestion.
 
Hello Andrew,

Actually, it's suggestion 1) that give the best clue, because the first
thing to run is the Form1 Constructor. Then the Application.Run is started,
beginning the message pump at this point, then the Form1.Load() is run.

Application.Run( new Form1() );
// Is equivalent to:
Form1 f1 = new Form1();
Application.Run( f1 );
// except this will hold a few extra bytes of memory.

When using
Application.Run( new Form1() );
the "new Form1()" is evaluated first, running the constructor and passing it
to the Application.Run as a parameter, and Run runs. It is after this that
the Load event is called. So, suggestion 1) and this way are really the
same thing, except suggestion 1) breaks it down the it's logical steps and
allows you to add intervening steps.

Chris R.
 
Back
Top