2

Is there a known, reliable way to delay a user logging off once they have clicked the logout button? I am wondering about how I could go about delaying, or possibly cancelling the logout then logout through the application once the counter has expired.

Prisoner
  • 27,391
  • 11
  • 73
  • 102

1 Answers1

2

When a logout is about to occur, the Windows message WM_QUERYENDSESSION is sent to all applicaitons. An application can reply to this message with a negative answer, asking for the logoff to be cancelled.

From the MDSN doc:

Applications should respect the user's intentions and return TRUE. By default, the DefWindowProc function returns TRUE for this message.

If shutting down would corrupt the system or media that is being burned, the application can return FALSE. However, it is good practice to respect the user's actions.

In .NET this functionality is exposed by the SystemEvents.SessionEnding event. If you set the Cancel property of SessionEndingEventArgs to true requests the logoff being cancelled.

The logoff part has been asked before in Log off user from Win XP programmatically in C#.

Community
  • 1
  • 1
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • Nitpicking, I know ;-), but the message is `WM_QUERYENDSESSION` (without the `S`) and after some seconds Windows will allow the user to kill those apps anyway, thus completing the logoff/shutdown. – Christian.K Aug 11 '11 at 16:26
  • Thats what I don't want to happen, the idea is that this box should remain on the top of all other windows. If that "kill processes" box then comes up it will be useless. – Prisoner Aug 11 '11 at 16:28
  • I thought the possibility to kill the apps was shown for apps not shutting down properly, but that the entire logoff was cancelled when an app asked for a cancel. If e.g. you have an unsaved excel file and logs off, excel asks "really exit without saving". If you press "Cancel" there I think that the logoff is cancelled. – Anders Abel Aug 11 '11 at 16:29