I am writing a windows form application in .net using C#.
I am running into a problem that if my program is running when the computer goes into the sleep and/or hibernate state (I am not sure at this time which one, or if both, cause the problem), when the machine wakes up again the program just hangs. The only way to exit out of it is to kill the process from the task manager.
This is, for obvious reasons, not the way I want the program to function. Even if I just shut the program down when it goes into these states, that would be fine, but I am not quite sure how to do this or if there is a more graceful way altogether of handling this.
Hibernate uses less power than sleep and when you start up the PC again, you're back to where you left off (though not as fast as sleep). Use hibernation when you know that you won't use your laptop or tablet for an extended period and won't have an opportunity to charge the battery during that time.
Hibernation is a mid-way between shutting down your machine and putting it to sleep. I would call it shutting down, but while saving data of whatever is going on. Because hibernating uses a negligible amount of power, it is also impervious to power cuts as the data is saved to the hard disk.
Hybrid sleep is a type of sleep state that combines sleep and hibernate. When you put the computer into a hybrid sleep state, it writes out all its RAM to the hard drive (just like a hibernate), and then goes into a low power state that keeps RAM refreshed (just like a sleep).
How to wake up the computer or monitor from Sleep or Hibernate mode? To wake up a computer or the monitor from sleep or hibernate, move the mouse or press any key on the keyboard. If this does not work, press the power button to wake up the computer.
You need:
using Microsoft.Win32;
And this is the code:
void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if(e.Mode == PowerModes.Suspend)
{
this.GracefullyHandleSleep();
}
}
This is what I went with.
Handling those events may be a work-around. But before applying this kind of work-around I'd try to figure out what the application was doing when the OS went into hibernate.
Occurs hanging although application was just idle?
Is the application doing some kind of low-level work (communication with device drivers or external hardware) that should not be interrupted?
Does the application use some kind of network connection?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With