Fade a Panel- Windows Forms

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

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.

How to detect Windows shutdown or logoff

Attach an event handler method to the SystemEvents.SessionEnding event, and your handler method will be called each time the event is raised. Handling this event will allow you to cancel the pending log off or shut down, if you wish. (Although that doesn't actually work like it sounds in current operating systems; for more information see the MSDN documentation here.)

If you don't want to cancel the event, but just react to it appropriately, you should handle the SystemEvents.SessionEnded event instead.

You must make sure that you detach your event handlers when the application is closed, however, because both of these are static events.

To detect logoff

For .NET see this question: Is there a way in c# to detect a Windows shutdown/logoff and cancel that action (after asking the user)

What is the proper way of handling logoff / shutdown / restart when the application has unsaved data?

What seems to be the accepted way is to display the save as dialog regardless.

Cancelling the shutdown, then resuming it later is most certainly not an option, for the reason you state and various others.

Since simply discarding the data is unacceptable, there really is no other options.

Well, except to save the data to a temporary file, then automatically restoring them the next time the program is run. Rather like MS Word after it has crashed. Actually, the more I consider it, the better it sounds.

Edit: There's yet another avenue, namely to save continously, the way eg. MS OneNote does. What has struck me before is that, provided you implement decent multilevel undo in your application, the whole manual saving business is actually somewhat dated - an anachronism from the days when disk operations were expensive and error-prone, nowadays mostly old habit.

But I'm digressing. Anyway, it's probably not applicable to your application, since I imagine it needs to be implemented from the beginning.

How cancel shutdown from a windows service C#

the solution was to detect the shutdown whith a winform : http://msdn.microsoft.com/fr-fr/library/microsoft.win32.systemevents.sessionending%28VS.80%29.aspx

but Windows kill other process before i can detect it, so it's not the better solution!

How can my program know that Windows was rebooted for updates

begin from vista we can use for this single call

RegisterApplicationRestart(L"some cmd line", RESTART_NO_CRASH|RESTART_NO_HANG);

if we run under XP, we can (as @GSerg propose) listen for WM_ENDSESSION message with

wParam != 0 && (lParam & ENDSESSION_CLOSEAPP) != 0

and register self restart under HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce (but not under Run !)

explorer.exe on any next start handle this by delete value key and exec application.



Related Topics



Leave a reply



Submit