Why won't Application.Exit() exit the application?

M

Mark Cranness

I have a Windows Forms application that does not exit when
Application.Exit() is called.

The problem is related to the Main() code used (below)

[STAThread]
static void Main() {
// Attempt login
DialogResult loginResult;
using (LoginForm loginForm = new LoginForm()) {
Application.Run(loginForm);
loginResult = loginForm.DialogResult;
}
// If login OK, then open main form
if (loginResult == DialogResult.OK) {
Application.Run(new MainForm());
}
}

Calling Application.Exit() (from a pushbutton or menu or
ThreadException handler) does not cause MainForm to close, or the app
to exit (the window remains open and active).

Calling Application.Run() twice (once for LoginForm and once for
MainForm) seems to be the root of the problem; If I don't first Run
the LoginForm, then Application.Exit() works as expected.

A simple workaround is to call Application.ExitThread instead, but I
am wondering why Application.Exit() does not work?

Thanks,
Mark
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Mark,

Yes, the problem is that you are calling Application.Run twice. I'll try to
find out why this cause Application.Exit to fail.
Anyway, in your application you should open your first form with ShowDialog
instead of calling Application.Run. This will solve your problem
 
H

Herfried K. Wagner [MVP]

* Mark Cranness said:
I have a Windows Forms application that does not exit when
Application.Exit() is called.

The problem is related to the Main() code used (below)

[STAThread]
static void Main() {
// Attempt login
DialogResult loginResult;
using (LoginForm loginForm = new LoginForm()) {
Application.Run(loginForm);
loginResult = loginForm.DialogResult;
}
// If login OK, then open main form
if (loginResult == DialogResult.OK) {
Application.Run(new MainForm());
}
}

Calling Application.Exit() (from a pushbutton or menu or
ThreadException handler) does not cause MainForm to close, or the app
to exit (the window remains open and active).

Calling Application.Run() twice (once for LoginForm and once for
MainForm) seems to be the root of the problem; If I don't first Run
the LoginForm, then Application.Exit() works as expected.

<msdn>
This method stops all running message loops on all threads and closes
all windows of the application. This method does not force the
application to exit. The Exit method is typically called from within a
message loop, and forces Run to return. To exit a message loop for the
current thread only, call ExitThread.
</msdn>

It seems that it doesn't stop all running message loops...
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Herfried,

If you try to close the first form by calling Aplpication.Exit. You'll see
that the second Application.Run just returns (it doesn't do anything). It
makes me think that the framework keeps internal flag that says the message
loop has been exited or something like that. So, maybe Application.Exit
checks this flag and decided that the loop has been already exited and
dosn't do anything.

This is just a wild guess.

--
B\rgds
100 [C# MVP]

Herfried K. Wagner said:
* Mark Cranness said:
I have a Windows Forms application that does not exit when
Application.Exit() is called.

The problem is related to the Main() code used (below)

[STAThread]
static void Main() {
// Attempt login
DialogResult loginResult;
using (LoginForm loginForm = new LoginForm()) {
Application.Run(loginForm);
loginResult = loginForm.DialogResult;
}
// If login OK, then open main form
if (loginResult == DialogResult.OK) {
Application.Run(new MainForm());
}
}

Calling Application.Exit() (from a pushbutton or menu or
ThreadException handler) does not cause MainForm to close, or the app
to exit (the window remains open and active).

Calling Application.Run() twice (once for LoginForm and once for
MainForm) seems to be the root of the problem; If I don't first Run
the LoginForm, then Application.Exit() works as expected.

<msdn>
This method stops all running message loops on all threads and closes
all windows of the application. This method does not force the
application to exit. The Exit method is typically called from within a
message loop, and forces Run to return. To exit a message loop for the
current thread only, call ExitThread.
</msdn>

It seems that it doesn't stop all running message loops...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top