9

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?

shA.t
  • 16,580
  • 5
  • 54
  • 111
Sébastien Nussbaumer
  • 6,202
  • 5
  • 40
  • 58

3 Answers3

10

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.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • 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. – Sébastien Nussbaumer Jan 07 '11 at 08:53
  • @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. – Adam Houldsworth Jan 07 '11 at 09:02
  • 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 ... – Sébastien Nussbaumer Jan 07 '11 at 09:34
4

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.

Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
  • 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? – Adam Houldsworth Jan 06 '11 at 18:36
  • 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. – Tim Robinson Jan 06 '11 at 23:13
  • 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 Jan 07 '11 at 09:00
  • 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). – warriorpostman Jan 19 '12 at 18:06
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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536