Detect and STOP shutdown in a VB.net 2003 Service

Howdy folks,

I believe I may have found the one thing that’s impossible to program (without "cheating"). I'm using VB.NET 2003, on a winXP Pro machine. Here's what I'm trying to do:

  1. Using a service without a GUI, I'd like to detect when Windows is shutting down, restarting, or logging off. I need to differentiate between shutdowns, restarts, and log offs.
  2. As soon as one of these actions is detected, I need to cancel/postpone it, do some tasks (which may take awhile), and then resume the shutdown/restart/log off.

This must be done with a service, using no windows or GUI. That means no cheating, like using "CreateWindowEx" APIs – that's a GUI.

I guess I'll probably have to settle for compromises, but if there is a solid solution out there, pleeeeeeease help!

Cheers!



Answer this question

Detect and STOP shutdown in a VB.net 2003 Service

  • Shawn Songer

    OK... I'll come with a bold suggestion: Make your process a service instead. A service exposes the OnStop and OnShutdown events. In these event handlers you can do your job, and then let the service stop and the OS to complete the logoff/restart/shutdown without you having to find out which action has been taken.

    So... the big question is: Do you really need to stop the logoff/restart/shutdown Or do you just want time enough to do any tear-down before the process is ended


  • Agus Ilmi Kurniawan

    I take your second point - but by the time they do so, hopefully they'll have provided adequate alternatives.

    On your first point - don't create a .net form - just create a simple window using the Windows API directly - the impact on size will be negligible, and you can then use WM_QUERYENDSESSION.

    Alternatively, if you really, really are sqeamish about using your own window, don't. FROM A DLL, hook the message loop and subclass the main desktop window, then examine it's messages.


  • Tomas Floyd

    Thanks Claes,

    That actually did help a bit, but I still can’t differentiate between a shutdown and a restart. Also, I can’t even detect or cancel the logoff event. Here’s what I’m doing:


  • Guillermo Díaz

    Why no GUI

    What's wrong with a hidden window for the purpose


  • Good-man

    Take a look on the Microsoft.Win32.SystemEvents Class. There is a SessionEnding event that you might find useful.


  • chpe

    Hey folks,

    Well, for starters, it actually is a service. OnShutdown “works,” but I still can’t differentiate between shutdowns, restarts, and it doesn’t even trap logoffs at all.

    I need to do all this because it has to be up to the user when to perform the long cleanup tasks: during shutdown (most likely choice), restart, or log off. The choice has to be the user.

    As for why no forms Well, two big reasons:

    • As it’s a service running all the time, the smaller the better. I’ve tested it with and without a form, and the size of the EXE and the running processes memory usage is insanely larger with just one blank, hidden form.
    • And because it’s a service, GUIs are shunned, and Microsoft has even threatened not to support them in future Windows versions

    Surely there must be an actual way to detect and postpone a shutdown, restart, and logoff in a service (while differentiating between the three).


  • Detect and STOP shutdown in a VB.net 2003 Service