我想在系统进入睡眠状态之前保存一些用户的工作,因此在进行了一些研究之后,我才知道SystemEvents.PowerModeChanged,但是由于这个事件,我的程序无法完成保存用户的工作,因此只有系统进入睡眠状态。我应该处理这种情况吗?
最佳答案
ManagementEventWatcher eventWatcher = new System.Management.ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("Win32_PowerManagementEvent"); //catch power change events
eventWatcher.Query = query;
eventWatcher.EventArrived += new System.Management.EventArrivedEventHandler(eventWatcher_EventArrived); // rgister the event handler
eventWatcher.Start(); // start the event watcher
处理程序:
void eventWatcher_EventArrived(object sender, System.Management.EventArrivedEventArgs e)
{
int eventType = Convert.ToInt32(e.NewEvent.Properties["EventType"].Value);
switch (eventType)
{
case 4://sleep
break;
case 7://resume
break;
}
}
注意:当程序退出时,请不要忘记停止监视程序。
if(eventWatcher != null)
eventWatcher.Stop();
更多细节 ...
http://cmehmet6parmak.wordpress.com/2011/06/14/catch-computer-sleep-event-c-windows-7/
关于c# - 如何在系统进入C# sleep 之前执行一组任务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26139655/