467,076 Members | 965 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

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

Prevent Windows from closing an application

I want to prevent a user from closing an application while doing critical
operations. The following code (concept came from MSDN) works except for
when the user attempts to terminate via "Task List" which causes "Program
not responding"...press "Cancel" and no issues, press "End now" and sure
enough it ends.

My question is; is there any method to prevent the "Application not
responding" message from appearing?

Current code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~
#Region " Windows Form Designer generated code "
Private bWasClick As Boolean
Private bAllowExit As Boolean
....
Public Const SC_CLOSE As System.Int32 = 61536
Public Const WM_SYSCOMMAND As System.Int32 = 274
Protected Overloads Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_CLOSE Then
Me.bWasClick = True
If Me.CheckBox1.Checked Then
Return
End If
End If
MyBase.WndProc(m)
End Sub

....

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles CheckBox1.CheckedChanged
bAllowExit = DirectCast(sender, CheckBox).Checked()
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If Me.bWasClick Then
'...
Else
'...
End If
e.Cancel = bAllowExit
End Sub

Visual Studio 1.1

Thanks for any assistance and for taking the time to read this question!
Kevin
Nov 21 '05 #1
  • viewed: 3217
Share:
2 Replies
Kevin,

When you simple use the e.cancel in the window closing event is it much
easier

(It is nice to show a messagebox before you set this to true) otherwise it
can be called a kind of bug.

Cor
Nov 21 '05 #2
I left the messagebox out for clarity...at this point it ask to proceede or
abort rather then be rude to simply say no.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uY**************@tk2msftngp13.phx.gbl...
Kevin,

When you simple use the e.cancel in the window closing event is it much
easier

(It is nice to show a messagebox before you set this to true) otherwise it
can be called a kind of bug.

Cor

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by alecjames1@hotmail.com | last post: by
7 posts views Thread by alan | last post: by
9 posts views Thread by Daniel Walzenbach | last post: by
2 posts views Thread by Strahimir Antoljak | last post: by
6 posts views Thread by Velvet | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.