473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Blocking app closing by user

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
Jul 23 '08 #1
10 1795
=== 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/libr...41(VS.85).aspx
WM_CLOSE: http://msdn.microsoft.com/en-us/libr...17(VS.85).aspx
Cheers,
Udo
Jul 23 '08 #2
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

"John" <in**@nospam.infovis.co.ukschreef in bericht
news:uo**************@TK2MSFTNGP06.phx.gbl...
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
Jul 23 '08 #3
Would disabling the Close button also disable Alt-F4?

Thanks

Regards

"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:F3**********************************@microsof t.com...
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

"John" <in**@nospam.infovis.co.ukschreef in bericht
news:uo**************@TK2MSFTNGP06.phx.gbl...
>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

Jul 23 '08 #4
=== 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
Jul 23 '08 #5
On Jul 23, 10:18 am, "John" <i...@nospam.infovis.co.ukwrote:
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/micro...b18cca5e507edb

Hope this helps,

Onur Güzel

Jul 23 '08 #6
On Jul 23, 3:18*am, "John" <i...@nospam.infovis.co.ukwrote:
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/
Jul 23 '08 #7
On Jul 23, 11:08 am, Udo Nesshoever <Brucklyn...@googlemail.com>
wrote:
=== 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
If you disable close box using the code in that thread or here;
http://groups.google.com/group/micro...b18cca5e507edb

...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
Jul 23 '08 #8
"Udo Nesshoever" <Br*********@googlemail.comschrieb:
>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?
If the close button is disabled, the shortcut is disabled too. However, you
cannot prevent the user from killing the process.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jul 23 '08 #9
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
"rowe_newsgroups" wrote:
On Jul 23, 3:18 am, "John" <i...@nospam.infovis.co.ukwrote:
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/
Jul 24 '08 #10
SurturZ wrote:
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
Jul 24 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ryan | last post by:
We have a situation that occurs every so often with blocking of various databases on one server (Win200 SQL7). It appears to happen at random, so I'm assuming it originates from something a user...
3
by: David Sworder | last post by:
This message was already cross-posted to C# and ADO.NET, but I forgot to post to this "general" group... sorry about that. It just occured to me after my first post that the "general" group readers...
1
by: Sagaert Johan | last post by:
Hi Ii have a simple server thread in an app that listens for connections, for some unclear reason an exception is thrown every now and then : 'A blocking operation was interrupted by a call to...
8
by: Mauricio | last post by:
Hello, Currently we have an ASP.NET 2003 app running, on one function the app calls to a stored procedure to SQLServerONE, that stored procedure creates some TEMP tables with the results of a...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
6
by: Joe HM | last post by:
Hello - I have a function that calls Thread.Abort() to stop a thread in a _Closed() Method of a GUI. The thread contains a blocking call on a TCP socket and that is the easiest way to stop...
3
by: cmo | last post by:
Well I hope I this isn't too nebulous of a problem. The problem I currently have is this: I have a button in a form that opens up a javascript/css poup that has an input field and two ahref links...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.