App behaviour when PC enters sleep

I have some apps that run daily, from 8 am to 8 pm, non-stop.

Sometimes these computers go to sleep and since the app is still open and I’m using MS SQL (means I’m connected with my app to SQL server), when PC comes out of sleep, app hangs and needs to be killed manually.

How do you guys deal with this? Is there a way to forcefully close the app (when it’s somewhere with some form open, it’s the worst, I know) before going into sleep? Or do you prevent sleep with your app?

Best would be if app would “survive” this sleep/wakeup as if nothing has happened but…

What are the options?


I seem to remember @Rick_UpperPark did a webinar either on ClarionLive or one of the CIDC sessions about this, I’ve tagged Rick so maybe he can answer.

if opening a browser is an option Screen Wake Lock API - Web APIs | MDN

It was CIDC “2020” (Africa): Lost SQL Connections by Rick Martin

Brilliant strategy, albeit involving a fair amount of extra SQL traffic reading column definitions on each new thread. I’ve used this in several SQL-based web apps that now run reliably for months at a time.
Thanks, Rick!


Thanks Jane.

Your memory is better than mine :smiley:

This brings the connection back to life after waking up from a sleep, or prevents sleep?

The basic premise is to use a unique connection string per thread. You can use the application name parameter of the connection string to accomplish this. The when each thread closes, it disconnects from the server. That way open connections are not kept forever. I’ve only used this solution in server/service programs.

However, you are describing the user leaving the browse or update form open and then the computer is going into sleep mode. That’s different. Unfortunately, I don’t think you are going to find a good resolution. Clarion just does not handle a lost connection to the server very well.

1 Like

One way would be to have your program prevent Sleep.

I’ve done it by using the API Keybd_Event() to send Keyboard Events that just press and release the Shift key. The GetLastInputInfo() API lets you check if the user has not done anything for X minutes and then press Shift.

I have some code that uses Capesoft’s WinEvent and Windows messages to help detect when the computer goes into sleep mode and when it wakes. That, along with the other suggestions, may help you. I’ll post the code when I get to my dev machine.

Awesome. I was thinking that what I would like the most is when going to sleep is detected, the app would close itself down ans allow sleep. That would be ideal I think.

Code to detect computer entering sleep mode:

WM_POWERBROADCAST                   Equate(536)

  case Event() - WinMessageEvent
  of 0 ! WinMessageEvent
    ! Start of "WinAlert Message Processing"
    ! [Priority 2500]
    Case WinParam1()
    Of PBT_APMPOWERSTATUSCHANGE    !Power status has changed.
    Of PBT_APMRESUMEAUTOMATIC      !Operation is resuming automatically from a low-power state. This message is sent every time the system resumes.
    Of PBT_APMRESUMESUSPEND        !Operation is resuming from a low-power state. This message is sent after PBT_APMRESUMEAUTOMATIC if the resume is triggered by user input, such as pressing a key.
    Of PBT_APMSUSPEND              !System is suspending operation.


These are the main ones to watch for. I tested this by running a test app on a laptop, put the laptop into sleep mode, and wrote trace statements to a TXT file.

This can come in real handy if running an app as a Service.

Hope this helps.


Thank you Don, really appreciate it!

1 Like