2

I wish to execute a piece of code in C# which updates a record in database before the system sleep happens.

I have tried finding it on Pinvoke but was not able to find it. Searching for Delay Sleep returns me the results for Thread.Sleep method.

Please help.

SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143
Pankaj Nikam
  • 665
  • 7
  • 22
  • you mean you have a application running and when the user chooses to put windows to sleep your application should execute that function? – Vajura Oct 29 '14 at 11:21
  • @Vajura, yes I have an application running and user chooses to Sleep the machine. I want to update a small record before he goes to the sleep mode. – Pankaj Nikam Oct 29 '14 at 11:23
  • That looks interesting :) – Nikhil Chavan Oct 29 '14 at 11:25
  • @NikhilChavan actually yes. It is a very peculiar condition where we have to update something before the sleep operation happens. – Pankaj Nikam Oct 29 '14 at 11:25
  • Maybe this one could help [Detect Power State Change](http://stackoverflow.com/questions/3948884/detect-power-state-change) – Markus Safar Oct 29 '14 at 11:27
  • @MarkusSafar, I tried that. It actually just notifies that the system got a suspend event. If I try to do a small Thread.Sleep over there to simulate my database call, the system goes to sleep even before I complete the sleep call. – Pankaj Nikam Oct 29 '14 at 11:33
  • Hm... I see. Actually when implementing a windows service there is the possiblity of requesting additional time, I have no idea if this helps you in any way [ServiceBase.RequestAdditionalTime](http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.requestadditionaltime%28v=vs.110%29.aspx) as you are not implementing a windows service. – Markus Safar Oct 29 '14 at 11:39
  • @MarkusSafar Sadly, my answer is no. I cannot implement a Service for that. Its just a windows application. – Pankaj Nikam Oct 29 '14 at 11:45
  • Try doing an actual database I/O instead of Thread.Sleep. But you may wind up implementing a small service instead. It's really very easy. – John Saunders Oct 30 '14 at 04:46
  • why not http://stackoverflow.com/questions/18206183/event-to-detect-system-wake-up-from-sleep-in-c-sharp – Nahum Oct 30 '14 at 05:32

3 Answers3

0

To do this you'll have to implement ServiceBase.OnPowerEvent Method

Tinwor
  • 7,765
  • 6
  • 35
  • 56
0

Microsoft.Win32.SystemEvents.PowerModeChanged event will give you this information.

prem
  • 3,348
  • 1
  • 25
  • 57
0

Please see this link to check whether Windows is going into/out of sleep or Hibernate mode

Community
  • 1
  • 1
SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143
  • I have tried it but it does not work as expected. You can check out my comment about the post in the question itself. – Pankaj Nikam Oct 31 '14 at 01:27