Application Revents Logoff or shutdown

J

Jm

Hi All

I have an app that when running if a user selects logoff or shutdown from
the start menu, it will close itself but not logoff windows or shutdown.
From what i have found so far its most likely something ive declared in my
program that is preventing the logoff, i have tried closing and disposing
forms and sql connections, is there anything else i should be looking for ?
Or some way to step through it to find what is causing this

Many thanks
 
C

Crouchie1998

' You need to look at consuming QUERYENDSESSION in WndProc to do what you
want

' Declaration:
' ------------

Private Shared WM_QUERYENDSESSION As Integer = &H11

' Paste in the following sub:
' --------------------------

#Region "Window Proc (SUB)"

<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissi
ons.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
' Listen for operating system messages
If m.Msg = WM_QUERYENDSESSION Then
' Handle the closing of your program here. If you have the form closing
to the System Tray then you need to handle the Form_Closing event (see
below)
End If
' Handle the message
MyBase.WndProc(m)
End Sub

#End Region

' When I minimise to System Tray, I set Cancel = True, but when Windows is
closing down, I make sure that Cancel = False allowing the closing of the
application. This can be down with a simple boolean value:

Dim blnIsWindowsClosing As Boolean = False

' In Form Closing:
' -----------------

If blnIsWindowsClosing = True Then
e.Canel = True
else
e.Cancel = False
End If

' So, now in the QUERYENDSESSION (WndProc), set :
' ----------------------------------------------------------

blnIsWindowsClosing = True

' Obviously, if you do not want to minimise to System Tray then just use the
QUERYENDSESSION constant & the WndProc sub

' Its as easy as that. It works for Logoff, Restart & Shutdown

I hope this helps

Crouchie1998
BA (HONS) MCP MCSE
 
M

M. Posseth

It sounds like a feature that someone else could be loocking for .......

:)

i would try to isolate this bug if i were you ,,,,, you might get good money
for it

:)
 
H

Herfried K. Wagner [MVP]

Jm said:
I have an app that when running if a user selects logoff or shutdown from
the start menu, it will close itself but not logoff windows or shutdown.
From what i have found so far its most likely something ive declared in my
program that is preventing the logoff, i have tried closing and disposing
forms and sql connections, is there anything else i should be looking for
?

You may want to watch for a shutdown and then terminate your application
yourself instead of waiting for Windows to do so.

<URL:http://dotnet.mvps.org/dotnet/samples/windowsandforms/CloseWindow.zip>

Take a look at 'SystemEvents.SessionEnding' too.
 
C

Cor Ligthert

JM,

What OS is the user using. It gives me slightly the idea that you are
talking about a user who has a W98/Me while the program is in a loop.

Cor
 
C

Crouchie1998

If you looked at my original code then that will do exactly what you need.

Crouchie1998
BA (HONS) MCP MCSE
 
L

lgbjr

Sorry to go off topic, but...

LOL! Every cloud has a silver lining!

Actually, the idea of having a program that inhibits logoff and shutdown
might be interesting. If you don't want someone to accidentally shutdown
your machine or log you off, you just lock your screen, but if it's a shared
machine that many users have access to....

Let's say you have a computer that is shared by several people in a
department. One user is running a critical app, so they don't want the
machine shutdown or their session logged off. So they run this app. The
other users could still "Switch User" to their accounts. The app would have
to run with admin privlidges, because it would have to inhibit shutdown from
all accounts, plus logoff from whichever account initiated the app.
Interesting.

Of course, no matter what the app is capable of, it couldn't prevent someone
from pulling the plug!

Lee
 

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