Windows Form Does Not Close When Using 'X' on ControlBox on Form

G

Guest

I was having an issue with my forms application. In the VS 2005 IDE When the
I selected the pulldown menu File->Exit my application would close normally,
but when when I would select the 'X' in the controlbox at the top, it would
leave the application in running mode, but wouldn't return back to the IDE.
Something was hanging somewhere. I tried cleaning up any unused objects or
items in the finalize and FormClosing events, but nothing worked.

I tried the following and it seems to work for me. Add the following line
to the FormClosing event...

Global.System.Windows.Forms.Application.Exit()

The only problem I have seen with this is that it actually executes the
FormClosing event twice, so I have to make sure that anything I do in the
FormClosing block checks to make sure it wasn't already done before. I'm not
sure if I have something wrong with my project, my code, or if something is
wrong with VS 2005 and the way it does all that magical stuff in the
background when using a ControlBox. Since I don't really see a way to
override that button easily, I found this to work just fine. I thought I
would post it for others if they run into the same problem.

Just another note... When doing this research, one of the most common
threads I saw was the question "How can I override the close on the
controlbox?". That one I have found to be easy. Just override the
FormClosing event as such...

Private Sub MyForm_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim oResult As MsgBoxResult = MsgBox("Tasks are still running in
the background. Are you sure you want to quit?", MsgBoxStyle.YesNo, "Quit all
tasks?")
If (oResult = MsgBoxResult.No) Then
e.Cancel = True
Exit Sub
End If
End Sub
 
J

Jeffrey Tan[MSFT]

Hi Jon,

I am not sure if you still need help on this issue. I want to help you find
out why clicking "X" controlbox on the form will not close the form.

Can you tell me what code you are execting in File->Exit menuitem handler
to implement the form closing function?

I suspect there is some code in your form that prevents the form from
closing while clicking the "X" of the form. Can you provide a sample
project to demonstrate this problem? It would be helpful for me to find out
the root cause. You may send the sample project to me at
(e-mail address removed)(remove "online.").

I will wait for your further reply, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Jeff, Thanks for the response. Let me see if I can try to duplicate the
problem in a test project. My project uses a lot of Backgroundworker threads
and other stuff that makes it a little more difficult to provide to you. It
has other class libraries I made etc. I'll try to get that to you within the
next couple days.

As requested...
I am using the following line of code in File->Exit , which always works
without a hitch...
Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e
As EventArgs) Handles ExitToolStripMenuItem.Click
Global.System.Windows.Forms.Application.Exit()
End Sub

Remember 2 things...
- I 'fixed' my problem (ok.. workaround, you got me) by adding the above
line to my FormClosing Event Handler.

-the only time my form doesn't close all the way is when I use the 'X' in
the controlbox. Any calls that I explicitly put in my project to close the
form work just fine. The code-behind stuff in the Microsoft Generated
sections don't do the exact same things as
Global.System.Windows.Forms.Application.Exit() does. I think somebody missed
something somewhere in there.

Again, I'll try to duplicate the problem with a much smaller and more
concise code set.
--
Jon Ebersole
Microsoft Certified Solution Developer


"Jeffrey Tan[MSFT]" said:
Hi Jon,

I am not sure if you still need help on this issue. I want to help you find
out why clicking "X" controlbox on the form will not close the form.

Can you tell me what code you are execting in File->Exit menuitem handler
to implement the form closing function?

I suspect there is some code in your form that prevents the form from
closing while clicking the "X" of the form. Can you provide a sample
project to demonstrate this problem? It would be helpful for me to find out
the root cause. You may send the sample project to me at
(e-mail address removed)(remove "online.").

I will wait for your further reply, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Jon,

Thanks for the feeback.

Yes, your information makes sense. Application.Exit method does not have
the same meaning as clicking the "X" controlbox on a form. The Exit method
stops all running message loops on all threads and closes all windows of
the application. While clicking the "X" controlbox on a form will post a
WM_CLOSE message to that form's WndProc. The normal result is closing that
form only and has no effect over other forms and threads in the application.

Also, internally, Application.Exit will calls ApplicationContext.ExitThread
on each thread to terminate the message loop of the threads. While if you
click the "X" controlbox on a form, the default WndProc of Form class will
handle the WM_CLOSE message and does the logic in Form.WmClose() method. If
you use Reflector to view the source code of Form.WmClose() method, you
will see that the processing for Form.WmClose() method will have a lot of
code in it. So these 2 ways of closing the form have very different code
path and meaning. It is likely certain party of the code in your
application will cause the Form.WmClose() method operation to stop, so that
it prevents the form from closing.

Actually, since Form.Close() method has almost the same logic as clicking
the "X" controlbox on the form, you may try to replace "Application.Exit()"
in File->Exit with "this.Close()". I suspect "this.Close()" can not close
the form either.

Anyway, with the current information available, it is still hard for me to
find out the root cause why the WM_CLOSE message can not close the form. A
little sample project is still a convenient way of troubleshooting the root
cause. I will wait for 2 more days for your sample project.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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