Link to home
Start Free TrialLog in
Avatar of Stef Merlijn
Stef MerlijnFlag for Netherlands

asked on

Disallow hibernation while Delphi application is running.

Hi,

Windows 10 and 11 will often shutdown connection to a MS SQL Server database when the computer is going into hibernation.

I want to disallow hibernation as long as my Delphi application is running, so the connection to the database remains active.

Of course I could ask each customer to execute "powercfg -h off", but I would like a little bit more control over this.

Avatar of arnold
arnold
Flag of United States of America image

disable the hibernation, sleep mode using the power management settings
what control do you have over customer equipment?
If this is part of an AD, you can use GPO.

more detail on your situation. do you have external people who connect to you and let the system go to sleep/hibernate such that when the user resumes, the application errors following loss of connection?

you could as part of the install process, run the changes for powermanagement.
I'm not a Delphi expert, but can't you execute "powercfg -h off" from it?
Avatar of Stef Merlijn

ASKER

Hi,

Of course I could Shellexecute a "powercfg -h off" command, but I would rather not change settings on the customer's computer.
I only want to prevent that, while running my Windows application, the connection to the MS SQL Server database gets lost.
I found the code below, but it doesn't seems to work in all instances. F.e. a user can close the cover of their laptop.
procedure WMPOWERBROADCAST(var Msg: TMessage); message WM_POWERBROADCAST;


procedure TFHoofdscherm.WMPOWERBROADCAST(var Msg: TMessage);
begin
  case Msg.WParam of
    PBT_APMBATTERYLOW:  //battery power is low - better shut down
    begin
      ApplicatieWordtAfgesloten;
      Application.Terminate;
      ExitProcess(0);
    end;
    PBT_APMSUSPEND, PBT_APMSTANDBY: //system is going to standby or hibernation, you have appx 20secs
    begin
      ApplicatieWordtAfgesloten;
      Application.Terminate;
      ExitProcess(0);
    end;
    PBT_APMQUERYSUSPEND, PBT_APMQUERYSTANDBY: //request to standby or hibernation
    begin
      Msg.Result := BROADCAST_QUERY_DENY // try to deny request
    end;
  end;
end;

Open in new window

Maybe I don't use it correctly? Do I need to execute it separately in order to activate it?
What you can do is add a handler in your application to chevron the presence of the SQL connection, and if missing reconnect
This way you do not need to control user's computer. Setting.
@Arnold: how can that be done?
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Arnold: "You would have to handle this withi the application's error handler. "

I've done exactly that and it works perfectly. Thank you very much.
Glad I could help