C# - How to detect a Windows shutdown/logoff and cancel that action (after asking the user)

18,551

Solution 1

There is a static class called SystemEvents that exposes this behaviour:

http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.aspx

However, it cannot differentiate between certain actions and doesn't pause the OS process time-out guard. I used it once, but the default time-out as configured in the registry is a little short so will likely need increasing.

To cut a long story short, it all felt a little hackish.

Solution 2

To add to @Adam's answer, if you need to tell the difference between logoff and shutdown/reboot, you can handle the WM_QUERYENDSESSION message.

"Shutdown Changes for Windows Vista" is a useful article for understanding the shutdown timeout.

Solution 3

Trying to block a shutdown is a lossy proposition these days, it's no longer possible to do so in Vista and up. A prompt isn't readable nor reachable. Using a service is highly indicated here, lets you survive a user log-off. And a reboot, your service will start running again automatically, letting you complete the job.

Share:
18,551

Related videos on Youtube

Sébastien Nussbaumer
Author by

Sébastien Nussbaumer

Currently developping microservices with : Spring Boot Spring Cloud Java Kafka Docker Linux Used to worked on : ASP.NET applications .NET desktop application iOS : Objective C and Flex Android : Java and Flex Windows

Updated on May 14, 2022

Comments

  • Sébastien Nussbaumer
    Sébastien Nussbaumer about 2 years

    Some explanation: for a project I'm working on I have to write a program that is running in the background, detects changes to files, and uploads the modified files to a web service to make it available to others. Quite simple synchronization if it were not for the case when a user modifies a big file and decides to shutdown its computer right after the edit.

    I could cancel the upload and wait for the next reboot to do the upload, but I can imagine the user downloading the file from the web to another computer the next morning and don't understanding why his changes from last night aren't there.

    So my idea was to detect when the users logs off or reboots Windows, and if I'm in the middle of an upload, just asking the user "We're still synchronizing file Foo.txt that you just changed. Are you sure you want to reboot ? You're changes won't be available to others until you restart your computer !". If the users says no, I'd need to cancel the reboot/loging off

    Is this possible?

  • Adam Houldsworth
    Adam Houldsworth over 13 years
    thanks for that. I haven't had chance to add it (had to catch my train) but my situation was telling the difference between shutdown and restart so that I could resume the user's action once I had handled aborting the work. I don't think it's possible to differentiate those two. I also think the events give you some information on the action?
  • Tim Robinson
    Tim Robinson over 13 years
    The SystemEvents class doesn't tell you what happened; WM_QUERYENDSESSION can only tell you the difference between logged off vs. powerdown or reboot. On Windows it's not possible to tell the difference between a powerdown and a reboot while the system is shutting down.
  • Sébastien Nussbaumer
    Sébastien Nussbaumer over 13 years
    OK that's what I thought... I'll let go of this behaviour then. Another idea I have is showing upload progress in a semi-transparent window at all times. The users is then made aware of the work that is going on behind the scene and probably won't shutdown in that case.
  • Adam Houldsworth
    Adam Houldsworth over 13 years
    ah yes. I found my old code last night and found I was using this message! Luckily, the code was a proof-of-concept for a previous boss and never saw the light of day.
  • Adam Houldsworth
    Adam Houldsworth over 13 years
    @Sebastien to be honest I would look at doing a service as Hans suggested. Make the upload robust such that it can be resumed, and wrap it all in a service so the user doesn't have to restart it all manually.
  • Sébastien Nussbaumer
    Sébastien Nussbaumer over 13 years
    That's more or less the behaviour I was going to adopt when I said "I'll let go of this behaviour". I was just thinking of adding a discrete upload progress just to let the user know. Granted this may not be needed if resumes are provided. I guess I'm just a bit nervous about the idea of a user going home with his notebook and files not backed up in the system ...
  • warriorpostman
    warriorpostman over 12 years
    I'm having a similar problem, except that I HAVE to distinguish between reboot and shutdown. @TimRobinson What might be the reason that this distinction is not provided? Also, is there a reason that Microsoft does not specifically state this (at least not as far as I've been able to google).