By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,639 Members | 2,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,639 IT Pros & Developers. It's quick & easy.

Prevent Windows from closing an application

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.