0

I want my C# Service to perform some tasks before a Windows shutdown. I see that there is an event SystemEvents.SessionEnding Event. However it does not seem to distinguish between a logoff and a system shutdown. Anyone know of a way of detecting a shutdown? Thanks.

This is not a duplicate of the following question since I am specifically referencing Shutdown not Shutdown|Logoff: How to detect Windows shutdown or logoff

Community
  • 1
  • 1
Neil Weicher
  • 2,370
  • 6
  • 34
  • 56
  • 4
    Possible duplicate of [How to detect Windows shutdown or logoff](http://stackoverflow.com/questions/6799955/how-to-detect-windows-shutdown-or-logoff) – huMpty duMpty Mar 15 '17 at 12:28
  • 3
    System shutdown is *not* the time to start doing work. (Most especially because you never get advanced notification of a power cut) – Damien_The_Unbeliever Mar 15 '17 at 12:30
  • 1
    There is a more suitable event to acces via WPF but as Damien says, its morerly irelevant to start work there. – cramopy Mar 15 '17 at 12:33
  • I agree to my previous commentators. However a lock can be a point where you want to do stuff. I myself had a use case where something had to happen when a user locks the pc ( automated logoff after a time ). When the system shuts down he won't be happy about you starting stuff since it can prevent shutdown and/or slow it down. Also it could be that windows doesn't care and just shuts down without you being finished. Maybe explain the use case, so we can suggesst better approaches. – Nico Mar 15 '17 at 12:43
  • Basically I want my service to send a WCF message to a server when the system is shutting down. Looks like I cannot distinguish between a shutdown or a logoff? – Neil Weicher Mar 15 '17 at 13:18
  • It is possible to distinguish with Hex codes that are sent by windows internally when performing one of those actions. The codes are listed here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa376868(v=vs.85).aspx A stackOverflow question which already uses and processes these is here: http://stackoverflow.com/questions/24726116/when-using-exitwindowsex-logoff-works-but-shutdown-and-restart-do-not He uses it to perform these actions though, in Order to catch them you have to catch https://msdn.microsoft.com/en-us/library/aa376890(VS.85).aspx – Nico Mar 15 '17 at 13:22
  • Are you really implementing a Windows service? Services are stopped when the system shuts down and you even get some time to do a bit of work before Windows will give up on your service and just shut down anyway. – Martin Liversage Mar 15 '17 at 13:28
  • @MartinLiversage sounds like a service to me. Informing a server when a pc is shut down would best be done by a service that runs in the background does nothing than wait for the event. If you know a better solution I would love to hear it since I plan to use a service for an use case close to this :) – Nico Mar 15 '17 at 13:48
  • @Nico: The solution is to react to the shutdown event of your service. In .NET you do this by overriding the [`ServiceBase.OnShutdown`](https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onshutdown(v=vs.110).aspx) method (or you can use a library like [TopShelf](http://topshelf-project.com/).) And the mention of logging off in the question makes me think that perhaps this question is not about a service but about a desktop app running when the user is logged on. – Martin Liversage Mar 15 '17 at 13:54

0 Answers0