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

Re: Blocking app closing by user by Udo

Udo
Wed Jul 23 02:36:24 CDT 2008

=== 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

Re: Blocking app closing by user by Cor

Cor
Wed Jul 23 02:46:45 CDT 2008

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" <info@nospam.infovis.co.uk> schreef in bericht
news:uoXLgRJ7IHA.1420@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
>


Re: Blocking app closing by user by John

John
Wed Jul 23 03:06:21 CDT 2008

Would disabling the Close button also disable Alt-F4?

Thanks

Regards

"Cor Ligthert[MVP]" <notmyfirstname@planet.nl> wrote in message
news:F330E742-9045-4F5C-8A45-17825C9005A7@microsoft.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" <info@nospam.infovis.co.uk> schreef in bericht
> news:uoXLgRJ7IHA.1420@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
>>
>



Re: Blocking app closing by user by Udo

Udo
Wed Jul 23 03:08:17 CDT 2008

=== 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

Re: Blocking app closing by user by kimiraikkonen

kimiraikkonen
Wed Jul 23 06:03:32 CDT 2008

On Jul 23, 10:18 am, "John" <i...@nospam.infovis.co.uk> wrote:
> Hi
>
> My app does a few processes and the app can not be closes mid way. Theref=
ore
> 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 wh=
at
> 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 =3D 512

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

' ---End---

Also:
http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_=
thread/thread/49919c41da712d4/34b18cca5e507edb?hl=3Den&lnk=3Dst&q=3DclassSt=
yle#34b18cca5e507edb

Hope this helps,

Onur G=FCzel


Re: Blocking app closing by user by rowe_newsgroups

rowe_newsgroups
Wed Jul 23 06:22:44 CDT 2008

On Jul 23, 3:18=A0am, "John" <i...@nospam.infovis.co.uk> wrote:
> Hi
>
> My app does a few processes and the app can not be closes mid way. Theref=
ore
> 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 wh=
at
> 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 =3D 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/

Re: Blocking app closing by user by kimiraikkonen

kimiraikkonen
Wed Jul 23 11:51:59 CDT 2008

On Jul 23, 11:08 am, Udo Nesshoever <Brucklyn...@googlemail.com>
wrote:
> =3D=3D=3D original message =3D=3D=3D
> from: Cor Ligthert[MVP]
> date: 23.07.2008 09:46
>
> > Don't use the cancel on the close. It makes your users insecure. Disabl=
e
> > 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/microsoft.public.dotnet.languages.vb/browse_=
thread/thread/49919c41da712d4/34b18cca5e507edb?hl=3Den&lnk=3Dst&q=3DclassSt=
yle#34b18cca5e507edb

=2E..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 =3D Keys.Alt + Keys.F4 Then
'Notify user
MessageBox.Show("Sorry, You can't exit")
'Stay alive
e.Handled =3D True
End If
End Sub

'---Code Ends-----

=2E.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=FCzel

Re: Blocking app closing by user by Herfried

Herfried
Wed Jul 23 14:02:48 CDT 2008

"Udo Nesshoever" <BrucklynBoy@googlemail.com> schrieb:
>> 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/>


Re: Blocking app closing by user by surturz

surturz
Wed Jul 23 23:24:01 CDT 2008

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.uk> wrote:
> > 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/
>

Re: Blocking app closing by user by Andrew

Andrew
Thu Jul 24 05:50:19 CDT 2008

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