469,934 Members | 1,941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

Exiting an application

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.
Nov 15 '05 #1
8 2406
Try using this:

YourFormName.Dispose();
YourFormName.Close();

Hope this helps! :)

Ann

"Andrew Warren" <an*******@discussions.microsoft.com> wrote in message
news:40****************************@phx.gbl...
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.

Nov 15 '05 #2
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
"Andrew Warren" <an*******@discussions.microsoft.com> wrote in message
news:40****************************@phx.gbl...
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.

Nov 15 '05 #3
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.

"Ann Marinas" <an*********@xtra.co.nz> wrote in message
news:uB**************@tk2msftngp13.phx.gbl...
Try using this:

YourFormName.Dispose();
YourFormName.Close();

Hope this helps! :)

Ann

Nov 15 '05 #4
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.

"Andrew Warren" <an*******@discussions.microsoft.com> wrote in message
news:40****************************@phx.gbl...
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.

Nov 15 '05 #5
I think you are right. :)

Thank you so much for correcting me. I really do appreciate it! :)
Ann
"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.

"Ann Marinas" <an*********@xtra.co.nz> wrote in message
news:uB**************@tk2msftngp13.phx.gbl...
Try using this:

YourFormName.Dispose();
YourFormName.Close();

Hope this helps! :)

Ann


Nov 15 '05 #6
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().
Nov 15 '05 #7
Thanks Dave. I rearranged the code to operate along the
lines of your first suggestion.
Nov 15 '05 #8
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.

"Andrew Warren" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
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().

Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by pnp | last post: by
3 posts views Thread by Bonj | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.