Recent

Author Topic: Question :Cross Platfporm way to detect Sleep Mode ?  (Read 9776 times)

Josh

  • Hero Member
  • *****
  • Posts: 1455
Question :Cross Platfporm way to detect Sleep Mode ?
« on: March 06, 2017, 09:29:55 pm »
Hi

Does anyone know of a cross platform way to detect if the computer the application is running on has gone to sleep.?

I have a thread that queries external devices for data; and if it does not retrieve the data it then adjust itself to try to regain communication; however when the computer goes to sleep the thread is still running but the external device is not communicating because the pc has gone to sleep; eventually the thread logic after trying various things to try to regain communication will eventually give up and force a dignified shutdown with message along the the lines of 'Lost communication, please restart...'


If I could detect when pc has entered sleep state and when it awakens then I could add this logic into the thread to pseudo sleep while the pc is sleeping.

Any ideas would be appreciated?
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Thaddy

  • Hero Member
  • *****
  • Posts: 18976
  • Glad to be alive.
Re: Question :Cross Platfporm way to detect Sleep Mode ?
« Reply #1 on: March 06, 2017, 09:39:34 pm »
https://en.wikipedia.org/wiki/Schr%C3%B6dinger's_cat
What you want is a cross platform way of solving the Schrödinger's cat paradox...
The only reliable way - to some extend - is use wake-on-lan or use the server logs.
If you want to know which computer is in the sleep state (and you are not the server) , you will probably wake it up.... Setup some web cams ;)
Normally the server would know which computers are logged in. The server can give you that information. It holds a list.

Funny... Silly question... If you are already on THE server you have that information. Otherwise, poll the server, not the computer that's asleep. You will wake it up....

Basically a server holds connect and disconnect status. And that is the important info for your problem.

Of course,if you are allowed, you can write a small daemon and install that on all the computers you are interested in that signals you when it goes to sleep and signals you when it wakes up. That can be done in a cross platform way with FPC.

Also if simple protocols like e.g ping are allowed (not often), you can ping w/o waking it up: you won't get a reply, but then again on most networks pings are dropped.

« Last Edit: March 06, 2017, 10:03:39 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

guest60499

  • Guest
Re: Question :Cross Platfporm way to detect Sleep Mode ?
« Reply #2 on: March 06, 2017, 10:04:13 pm »
Unfortunately it looks like you will have to roll your own cross platform layer if you want actual OS-level events for going to sleep and waking up.

See:
The last one, asked in the context of Java, is the most general method and the one I was going to recommend if you want portability.

Thaddy

  • Hero Member
  • *****
  • Posts: 18976
  • Glad to be alive.
Re: Question :Cross Platfporm way to detect Sleep Mode ?
« Reply #3 on: March 06, 2017, 10:14:04 pm »
Unfortunately it looks like you will have to roll your own cross platform layer if you want actual OS-level events for going to sleep and waking up.
No. network protocols are all standardized.
In my daemon suggestion you use simple network communication. Can use the status flags from IP for all I care.
And to make a cross platform daemon or service is really easy in Freepascal.

The easy way on a local network is the server logs, though.

On the internet it is much more difficult without clientside software and wake-on-lan and ping  are only half the story.......
« Last Edit: March 06, 2017, 10:20:28 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Josh

  • Hero Member
  • *****
  • Posts: 1455
Re: Question :Cross Platfporm way to detect Sleep Mode ?
« Reply #4 on: March 06, 2017, 11:32:46 pm »
Hi

Thanks for responding, I think however I need to re-phrase my question.

My application communicates with USB hardware that when pc goes to sleep the hardware also goes to sleep, and does not receive/send any information.

The thread in my application is still running when in sleep mode; and because it gets no information it then tries different approaches to send/receive information until it finally gives up; think the device is not working or has been unplug.

If I can detect when the pc goes to sleep and awakens I can then recode the thread to stop its work while pc is in sleep mode; and only resume its work when pc is awake
ie
Code: [Select]
while not terminated do
begin
  if sleeping then  // I would need so functions either by timer or via message callback that can set and unset this sleeping variable
  begin
     // sleep thread for abit so as not to hog cpu
    Tthread.Sleep(20);
  end
  else
  begin
      ......................
     ...........................
     // normal thread code
  end;
end;

Sometimes its not easy to describe exactly what your trying to accomplish, at the moment I do not have any even slightly workable code; so even ideas that can be investigated would be good, I have come across some wm_????/ window message events that could be intercepted but these i assume would be win api specific.

Again apologies for the 'mis-direction'.

Josh
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

balazsszekely

  • Guest
Re: Question :Cross Platfporm way to detect Sleep Mode ?
« Reply #5 on: March 07, 2017, 05:38:53 am »
Quote
@Josh
The thread in my application is still running when in sleep mode;
That's not possible. Just before sleep, all running programs/threads are suspended, their states saved to hdd, then the computer goes into a low power state.

Thaddy

  • Hero Member
  • *****
  • Posts: 18976
  • Glad to be alive.
Re: Question :Cross Platfporm way to detect Sleep Mode ?
« Reply #6 on: March 07, 2017, 07:48:29 am »
That's indeed NOT possible.

Well,on the local pc with the USB hardware attached there are events to alert you that the pc is about to enter sleep mode and just after it awakes. (Both Linux and Windows at least)
If you want to monitor these events remotely, you can send these events via a Daemon to the control PC. That Daemon or service can control and hold up sleep until a notification is send to the control PC.
In Windows that (very simple) service  would listen for WMQueryEndSession, do ShutdownBlockReasonCreate, (notify the controlling computer here) , and do ShutdownBlockReasonDestroy. In Linux there is something similar, but I have to look that up.

You could also listen for WM_POWERBROADCAST message in your service and check for PBT_APMSUSPEND and PBT_APMRESUMESUSPEND. (Dunno for sure how that works)
For startup I suspect that it is enough to send a notification from the service that the service has started or resumed to the controlling computer, provided the service is started after the network services.

But anyway on Windows it's pretty straightforward. I will investigate a Linux solution.
[edit]
I found this http://stackoverflow.com/questions/8197477/how-to-make-java-app-detect-that-the-linux-pc-has-resumed-from-suspended-state
which seems doable in FPC.

Don't use Lazarus components to write this. Use pure FPC.
« Last Edit: March 07, 2017, 08:45:16 am by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

guest60499

  • Guest
Re: Question :Cross Platfporm way to detect Sleep Mode ?
« Reply #7 on: March 07, 2017, 03:23:32 pm »
Unfortunately it looks like you will have to roll your own cross platform layer if you want actual OS-level events for going to sleep and waking up.
No. network protocols are all standardized.
In my daemon suggestion you use simple network communication. Can use the status flags from IP for all I care.
And to make a cross platform daemon or service is really easy in Freepascal.

The easy way on a local network is the server logs, though.

On the internet it is much more difficult without clientside software and wake-on-lan and ping  are only half the story.......

What you are proposing has nothing to do with detecting whether the computer a program is running has gone to sleep or not.

Hi

Thanks for responding, I think however I need to re-phrase my question.

My application communicates with USB hardware that when pc goes to sleep the hardware also goes to sleep, and does not receive/send any information.

The thread in my application is still running when in sleep mode; and because it gets no information it then tries different approaches to send/receive information until it finally gives up; think the device is not working or has been unplug.

To test this you should log the date each time you process a serial event, continue sending data from the device, and then put the computer to sleep. I suspect you will see the failures to read when your computer wakes back up however it is possible but unlikely that the USB hardware is shut off before the processor goes to sleep, or rather, it takes long enough for the processor to go all the way to sleep after shutting off external devices that you can still try to read from them. Testing will let you know.

Josh

  • Hero Member
  • *****
  • Posts: 1455
Re: Question :Cross Platfporm way to detect Sleep Mode ?
« Reply #8 on: March 11, 2017, 02:45:30 am »
Hi

Sorry for delay in replying weekends is my normal time to develops apps.

I have done some testing and it appears that the issue is arising before the CPU goes to sleep,
The delay in the OS between the OS putting the USb Hardware to sleep and the actual CPU going to sleep is such that my thread thinks the hardware has become unresponsive; the delay is quite a few seconds ( probably due to shutting hardware down, saving state to harddrive and finally sleeping the CPU).

So I think I need to find a way to detect the moment the OS starts its SLEEP routine and when the AWAKEN mode has completely finished.

WM_POWERBROADCAST looks like a starting point for windows;
osx versions seem to be
NSWorkspaceDidWake
NSWorkspaceWillSleep
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

 

TinyPortal © 2005-2018