How to detect Windows shutdown or logoff
49117 просмотра
2 ответа
I need to detect when Windows is shutdown (or restarted) or when the user is logging off. I need to properly close the application before the application is closed. I noticed that no exit application event is raised when Windows is closing day.
I read the post Is there a way in c# to detect a Windows shutdown/logoff and cancel that action (after asking the user)
but I'm not sure of where I should perform the operations before closing. Thanks.
Автор: Andrea Nagar Источник Размещён: 13.11.2019 11:49Ответы (2)
60 плюса
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.
Автор: Cody Gray Размещён: 23.07.2011 11:0211 плюса
You can use a native solution via pinvoke if your code is not running in a non-interactive session (such as a system service):
//SM_SHUTTINGDOWN = 0x2000
bool bShutDownPending = GetSystemMetrics(SM_SHUTTINGDOWN) != 0;
Автор: c00000fd
Размещён: 14.10.2013 06:59
Вопросы из категории :
- c# Преобразовать десятичную в двойную?
- c# Как рассчитать чей-то возраст в C #?
- c# Как вы сортируете словарь по значению?
- c# В чем разница между int и Integer в Java и C #?
- .net Как создать новый экземпляр объекта из Типа
- .net Действительно ли опечатанные классы действительно предлагают преимущества?
- windows Эквивалент Windows «хороший»
- windows Реестр или INI-файл для хранения пользовательских настроек приложения
- windows Как я могу разработать для iPhone, используя машину для разработки Windows?
- windows Как настроить OpenFileDialog для выбора папок?
- winforms Как заставить кнопку выглядеть так, как будто она нажата?
- winforms Лучший способ получить доступ к элементу управления в другой форме в Windows Forms?
- winforms Можете ли вы использовать общие формы в C #?
- winforms Как отключить Alt + F4 закрытие формы?
- system-shutdown C # - Как обнаружить Windows завершение / выход из системы и отменить это действие (после запроса пользователя)
- system-shutdown Обнаружение ожидающего завершения работы Linux
- system-shutdown How to detect Windows shutdown or logoff
- system-shutdown shutting down computer (linux) using python