Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ascertaining a shutdown is closing my application

Tags:

c#

.net

shutdown

Is it possible in .NET to ascertain whether my application is closing due to Windows being given a shutdown command (as opposed to any old application closing) in order to either write out some temporary cache files or even block the shutdown long enough to prompt for user input?

Whilst my current scope involves a Winform app and a windows service, I am interested in understanding this in a generic way if possible

like image 728
johnc Avatar asked Oct 28 '08 22:10

johnc


2 Answers

SystemEvents.SessionEnding looks like a good starting point for you. That article talks about the event sequence involved when a logout/shutdown is occurring.

like image 68
Harper Shelby Avatar answered Sep 23 '22 01:09

Harper Shelby


In general, you will want to handle the WM_QUERYENDSESSION Windows message. This will give your application a chance to do cleanup, or to block the shutdown if it's really necessary.

like image 33
Greg Hewgill Avatar answered Sep 24 '22 01:09

Greg Hewgill