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

Aborting a shutdown with a windows service

P: n/a
I'm trying to write a Windows Service that is capable of detecting when a
shutdown is occurring, and cancelling it.

I've managed to get this to work fine in a windows application, using
SystemEvents.SessionEnding, like so: -

Private Sub MyBase_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

AddHandler SystemEvents.SessionEnding, AddressOf
SystemEvents_SessionEnding

End Sub

Private Sub SystemEvents_SessionEnding(ByVal sender As Object, ByVal e As
Microsoft.Win32.SessionEndingEventArgs)

e.Cancel = True

End Sub

but I haven't been able to get this method to work in a Windows Service.
I've tried putting the "AddHandler" line in various places, but even when
the code that it is in definitely runs (e.g. in the OnStart sub), any code
in the associated subroutine fails to run when I shut down. I don't know if
I'm adding the handler incorrectly for a service, or if I'm using totally
the wrong thing.
Also, I managed to find that another way of cancelling a shutdown would be
to override WndProc, catch WM_QUERYENDSESSION, and return a value that would
cancel the shutdown. However, I'm not sure how to build that into my
service either. I know I need to override the sub WndProc, but which class
containing the sub do I need to inherit for it to work in a service? And
how do I return a value to it to tell it not to shut down?
Any help on either point, or any other suggestions how I might do this would
be very much appreciated!

Thanks
Mat
Mar 21 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.