Prevent Windows from closing an application

  • Thread starter kevininstructor
  • Start date
K

kevininstructor

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
 
C

Cor Ligthert

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
 
K

kevininstructor

I left the messagebox out for clarity...at this point it ask to proceede or
abort rather then be rude to simply say no.
 

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