Blocking app closing by user

J

John

Hi

My app does a few processes and the app can not be closes mid way. Therefore
I want to be able to block any attempt of closing app by user such as by
clicking on close (cross) or Alt-F4 and once the app is finished doing what
it is doing to close. In other words I need to close the app gracefully. How
can I achieve this?

Thanks

Regards
 
U

Udo Nesshoever

=== original message ===
from: John
date: 23.07.2008 09:18
My app does a few processes and the app can not be closes mid way. Therefore
I want to be able to block any attempt of closing app by user such as by
clicking on close (cross) or Alt-F4 and once the app is finished doing what
it is doing to close. In other words I need to close the app gracefully. How
can I achieve this?

If the app has a main window: handle the FormClosing event and set
e.Cancel = true;

Otherwise handle the windows messages and check for
WM_QUERYENDSESSION: http://msdn.microsoft.com/en-us/library/aa376890.aspx
WM_QUIT: http://msdn.microsoft.com/en-us/library/ms632641(VS.85).aspx
WM_CLOSE: http://msdn.microsoft.com/en-us/library/ms632617(VS.85).aspx


Cheers,
Udo
 
C

Cor Ligthert[MVP]

John,

Don't use the cancel on the close. It makes your users insecure. Disable the
close button.

As you search this newsgroup with Google you will find a lot of
implementation for that.

Cor
 
U

Udo Nesshoever

=== original message ===
from: Cor Ligthert[MVP]
date: 23.07.2008 09:46
Don't use the cancel on the close. It makes your users insecure. Disable
the close button.

And how would that prevent closing the app with e.g. Alt+F4?

Cheers,
Udo
 
K

kimiraikkonen

Hi

My app does a few processes and the app can not be closes mid way. Therefore
I want to be able to block any attempt of closing app by user such as by
clicking on close (cross) or Alt-F4 and once the app is finished doing what
it is doing to close. In other words I need to close the app gracefully. How
can I achieve this?

Thanks

Regards

Hi,
This has been discussed much before:

Use that code in your form class to disable red close box:

' ---Begin---
Private Const CP_NOCLOSE_BUTTON As Integer = 512

Protected Overrides ReadOnly Property CreateParams() _
As System.Windows.Forms.CreateParams
Get
Dim noclose As CreateParams
noclose = MyBase.CreateParams
noclose.ClassStyle = CP_NOCLOSE_BUTTON
Return noclose
End Get
End Property

' ---End---

Also:
http://groups.google.com/group/micr...db?hl=en&lnk=st&q=classStyle#34b18cca5e507edb

Hope this helps,

Onur Güzel
 
R

rowe_newsgroups

Hi

My app does a few processes and the app can not be closes mid way. Therefore
I want to be able to block any attempt of closing app by user such as by
clicking on close (cross) or Alt-F4 and once the app is finished doing what
it is doing to close. In other words I need to close the app gracefully. How
can I achieve this?

Thanks

Regards

I vote you let the user close the form, but you keep the processes
running on a different thread that has IsBackground = False. That
should (it's been a while since I've done it) let the form die
immediately, but still keep the application alive and allow the
processes to finish and then shut themselves down.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
K

kimiraikkonen

=== original message ===
from: Cor Ligthert[MVP]
date: 23.07.2008 09:46


And how would that prevent closing the app with e.g. Alt+F4?

Cheers,
Udo

If you disable close box using the code in that thread or here;
http://groups.google.com/group/micr...db?hl=en&lnk=st&q=classStyle#34b18cca5e507edb

...users won't be able to close the application using ALT+F4.


However if you want to disable ALT+F4 without disabling close box (red
one), you can implement in that way:

First set form's KeyPreview property to True, then use form's KeyDown
Event:


'----Code Begins-----

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyData = Keys.Alt + Keys.F4 Then
'Notify user
MessageBox.Show("Sorry, You can't exit")
'Stay alive
e.Handled = True
End If
End Sub

'---Code Ends-----

..and remember, with that way, users will be allowed to quit using X
(close) button, but not with ALT+F4 combination if you're looking only
for this.

Hope this helps,

Onur Güzel
 
H

Herfried K. Wagner [MVP]

Udo Nesshoever said:
And how would that prevent closing the app with e.g. Alt+F4?

If the close button is disabled, the shortcut is disabled too. However, you
cannot prevent the user from killing the process.
 
S

SurturZ

I think the better solution is to restructure your program so that the user
can shut down whenever they want. Only start writing to disk (or whatever)
once all user input for the transaction is complete.

Generally a user can't click "X" unless you let them anyway. i.e. if you
have a loop running, the "X" event won't be handled until the loop closes.

As previously mentioned (by Cor I think) you can't stop a user pulling the
plug on the computer or killing the task in Taskman*. So it is better to give
the User a "Cancel" button to allow your long process to exit gracefully,
rather than to lock the UI, which only encourages them to kill the task or
reboot.

* Actually I think you CAN stop this, but you most certainly shouldn't.

--
David Streeter
Synchrotech Software
Sydney Australia
 
A

Andrew Morton

SurturZ said:
As previously mentioned (by Cor I think) you can't stop a user
pulling the plug on the computer or killing the task in Taskman*. So
it is better to give the User a "Cancel" button to allow your long
process to exit gracefully, rather than to lock the UI, which only
encourages them to kill the task or reboot.

* Actually I think you CAN stop this, but you most certainly
shouldn't.

For programs that resist death by Task Manager, there's always Process
Explorer.

Andrew
 

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