How to complete a set of tasks before the system goes to sleep in C #

I want some users to work before the system goes to sleep, so after doing some research I found out about SystemEvents.PowerModeChanged , but with this event my program does not exit with saved users and until that only the system is going to sleep. So how should I handle this scenario?

+3


source to share


1 answer


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

      

Handler:

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;
        }
}

      

Note. Remember to stop the watcher when your program exits.



if(eventWatcher != null)
     eventWatcher.Stop();

      

More information ...

http://cmehmet6parmak.wordpress.com/2011/06/14/catch-computer-sleep-event-c-windows-7/

+1


source







All Articles