2

I have a WPF program that communicates with a specialized USB stick that is collecting data (in fact an ANT USB dongle). I noticed that the data collection simply stopped after a few hours. The reason was evident in the windows logs (system) where at the exact time the program stopped getting data, I see: The system is entering sleep Sleep Reason: System Idle

Questions

  1. How do I programmatically prevent Windows from going to sleep so that I can continue to gather data?

2. Stepping backwards for the big picuture view... What's going on? Why does the computer going to sleep affect my program? Or is it just affecting the USB stick? Is it necessary to prevent sleep or should I do something else instead?

Einstein's answer is tantalizingly close. I just can't seem to get SetThreadExecutionState working in my C#/WPF program and can't find a lot of examples or discussions of it. Does it need to be called more than once? If so how? Is there a event that I receive that tell me to call it or should I call it every so often (5 minutes?) as suggested in:http://stackoverflow.com/questions/5870280/setthreadexecutionstate-is-not-working-when-called-from-windows-service For now, I'm just going into ctrl panel -> power options and preventing sleep but it would sure be nice to have an elegant solution. Even on my own computer, I don't want to mess with the sleep settings. It's too hard to remember to set them back again!

Dave
  • 8,095
  • 14
  • 56
  • 99

3 Answers3

2

You can prevent the computer from entering low power modes (sleep/suspend) by using the SetThreadExecutionState function.

As far as why going into low power mode is interrupting your data collection - well Windows suspends all processes in these modes and USB ports enter low power mode which likely means your USB device will not have power either. It's by design. After all, the whole reason we want our computers to go to sleep is so that the battery is not drained.

Josh
  • 68,005
  • 14
  • 144
  • 156
  • I had to do this with one of our data conversion applications. It was timing out on some of our clients machines when they went to sleep. – tsells Sep 29 '11 at 05:02
  • Thanks Josh, Is there a C# command to do this or must one you PInvoke as in this stackoverflow post: http://stackoverflow.com/questions/5870280/setthreadexecutionstate-is-not-working-when-called-from-windows-service Also, must I call it every 5 minutes like they state in same post? Thanks, Dave – Dave Sep 29 '11 at 13:14
1

You can apply the above responses, o simply you can go in Control Panel -> Power Options and modify the settings, so your system never goes to sleep.

garzanti
  • 2,162
  • 1
  • 22
  • 30
  • Thanks garzanti. I did think of that and it would work most of the time as this is just diagnostic code (not-commercial). But I do give it out to a limited subset of people and it would be difficult to go tho Control Panal for everyone. – Dave Sep 29 '11 at 13:07
  • @Dave It was a simple solution for a complex problem :), of course it cannot be applied always. – garzanti Sep 29 '11 at 18:29
0

To 1): You can use SystemParametersInfo with one of the power related values on Windows versions less than Vista to turn on/off power savings settings. Starting with Vista, you should register for one of the power events instead, and the OS will notify you when it needs to for the event you request.

To 2): If the OS shuts down, the hardware it's managing shuts down. What else would you expect to happen? If the OS runs the USB device driver, which runs the USB device, what would you think would happen if the OS goes to sleep? The USB device begins running itself instead without the driver?

Ken White
  • 123,280
  • 14
  • 225
  • 444