17

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
EatATaco
  • 687
  • 7
  • 25
  • Not sure why you mentioned visual studio. Is the problem only occurring when you are debugging in visual studio and you hibernate / sleep? Or have you verified that it happens with a release cut with VS closed? – Allen Rice Aug 06 '10 at 15:52
  • 1
    Maybe it is extraneous information, but I just like to give as much (what I believe to be) relevant information as I can when presenting a question. To answer your question, it definitely happens on the release version on a machine that does not even have visual studio on it. – EatATaco Aug 06 '10 at 16:09
  • 3
    Sorry, I am no expert programmer, I make no claims otherwise. That being said, I program using VS so, considering I don't know exactly what the problem is, to me it could very well be relevant information. Remember, we are asking questions because we are not experts. – EatATaco Aug 19 '10 at 17:43

3 Answers3

9

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.

EatATaco
  • 687
  • 7
  • 25
6

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?

  • It does poll a serial port every second and I use the built in serialport class. My gut tells me that this is what is causing the problem as I have had a number of issues with known bugs in the VS serialport class. But even then, the quickest solution is to just shutdown the program and we have been under a lot of time pressure recently so I will probably just do that to solve ALL the potential problems. – EatATaco Aug 06 '10 at 18:14
  • OK. Try closing the serial port when you receive hibernate/sleep/standby and open it after the OS is up again. – Christian Schwarz Aug 06 '10 at 20:08
2

This article covers listening for those events. You'll have to do something like override WndProc and listen for PBT_APMSUSPEND events.

msergeant
  • 4,771
  • 3
  • 25
  • 26