Recent

Author Topic: Prevent computer logging out  (Read 1306 times)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1185
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Prevent computer logging out
« Reply #15 on: February 02, 2023, 09:08:41 pm »
...
Put it outside of a method(!)
Use it not in a click, use it in a timer(!)

Exemplary:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes , SysUtils , Forms , Controls , Graphics , Dialogs , ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Timer1: TTimer;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure Timer1Timer(Sender: TObject);
  18.   strict private
  19.  
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. const
  34.  ES_CONTINUOUS       = $80000000;
  35.  ES_DISPLAY_REQUIRED = $00000002;
  36.  ES_SYSTEM_REQUIRED  = $00000001;
  37.  
  38. function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.Timer1Timer(Sender: TObject);
  43. begin
  44.   SetThreadExecutionState( ES_SYSTEM_REQUIRED );
  45.   SetThreadExecutionState( ES_DISPLAY_REQUIRED );
  46. end;
  47.  
  48. procedure TForm1.FormCreate(Sender: TObject);
  49. begin
  50.   Timer1.Enabled := True;
  51.   Timer1.Interval := 5000;
  52. end;
  53.  
  54. end.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10578
  • FPC developer.
Re: Prevent computer logging out
« Reply #16 on: February 03, 2023, 10:36:41 am »
Quote from: KodeZwerg link=topic=62135.msg469562#msg469562 date=1675365498
You need the SetThreadExecutionState like this:
[code=pascal
const
 ES_CONTINUOUS       = $80000000;
 ES_DISPLAY_REQUIRED = $00000002;
 ES_SYSTEM_REQUIRED  = $00000001;

function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';[/code]

Added to trunk.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 811
Re: Prevent computer logging out
« Reply #17 on: February 03, 2023, 11:22:06 am »
...
Put it outside of a method(!)
Use it not in a click, use it in a timer(!)

Exemplary:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes , SysUtils , Forms , Controls , Graphics , Dialogs , ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Timer1: TTimer;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure Timer1Timer(Sender: TObject);
  18.   strict private
  19.  
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. const
  34.  ES_CONTINUOUS       = $80000000;
  35.  ES_DISPLAY_REQUIRED = $00000002;
  36.  ES_SYSTEM_REQUIRED  = $00000001;
  37.  
  38. function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.Timer1Timer(Sender: TObject);
  43. begin
  44.   SetThreadExecutionState( ES_SYSTEM_REQUIRED );
  45.   SetThreadExecutionState( ES_DISPLAY_REQUIRED );
  46. end;
  47.  
  48. procedure TForm1.FormCreate(Sender: TObject);
  49. begin
  50.   Timer1.Enabled := True;
  51.   Timer1.Interval := 5000;
  52. end;
  53.  
  54. end.
Docs say, that Prev := SetThreadExecutionState(ES_CONTINUOUS or ES_SYSTEM_REQUIRED or ES_DISPLAY_REQUIRED) is enough, so timer isn't needed. But this state should be manually cleared via SetThreadExecutionState(Prev).
29.12.2021 - migration to DynamicData 4.1 is completed - complete overhaul of data access driver.
My project still requires full Delphi 2009 support to be ported to Lazarus.
It's time to finally do it, because Delphi 2009 is 14 years old.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1185
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Prevent computer logging out
« Reply #18 on: February 03, 2023, 12:28:31 pm »
Added to trunk.
Good day, than please like that and not my quick and dirty version  :-*
I was not aware of your action.  :o :-[
Code: Pascal  [Select][+][-]
  1. {$IFDEF MSWINDOWS}
  2. type
  3.   EXECUTION_STATE = UINT32; //4 byte unsigned int
  4.  
  5. const
  6.   //Away mode should be used only by media-recording and media-distribution
  7.   //applications that must perform critical background processing on desktop
  8.   //computers while the computer appears to be sleeping.
  9.   ES_AWAYMODE_REQUIRED = UINT32($00000040); //Enables away mode. This value must be specified with ES_CONTINUOUS.
  10.   ES_SYSTEM_REQUIRED = UINT32($00000001); //Forces the system to be in the working state by resetting the system idle timer.
  11.   ES_DISPLAY_REQUIRED = UINT32($00000002); //Forces the display to be on by resetting the display idle timer.
  12.   ES_CONTINUOUS = UINT32($80000000);
  13.  
  14. //If the function succeeds, the return value is the previous thread execution state.
  15. //If the function fails, the return value is NULL.
  16. //Minimum requirements: Windows XP
  17. function SetThreadExecutionState(esFlags: EXECUTION_STATE): EXECUTION_STATE; stdcall; external 'kernel32.dll';
  18. {$ENDIF}
That is the more better and full variant  O:-)

so timer isn't needed
I do not know to what document you refer, it is just wrong.
Not believe?
Make a test app that set value just once and do nothing, depending on your power configuration your system will go into power saving mode.
« Last Edit: February 03, 2023, 12:49:12 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Nicole

  • Hero Member
  • *****
  • Posts: 740
Re: Prevent computer logging out
« Reply #19 on: February 03, 2023, 01:37:28 pm »
have not read all the answers and risk to give mine twice:
There is a small tool by MS called "MouseMover" or similar.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1185
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Prevent computer logging out
« Reply #20 on: February 03, 2023, 03:29:18 pm »
have not read all the answers
You not read any answer since what you suggested is already in first reply.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 811
Re: Prevent computer logging out
« Reply #21 on: February 03, 2023, 07:05:56 pm »
I do not know to what document you refer, it is just wrong.
Not believe?
Make a test app that set value just once and do nothing, depending on your power configuration your system will go into power saving mode.
The most obvious docs - are Microsoft's ones.
29.12.2021 - migration to DynamicData 4.1 is completed - complete overhaul of data access driver.
My project still requires full Delphi 2009 support to be ported to Lazarus.
It's time to finally do it, because Delphi 2009 is 14 years old.

Davidous

  • Full Member
  • ***
  • Posts: 106
Re: Prevent computer logging out
« Reply #22 on: February 04, 2023, 08:37:00 am »
Thank you All!

I implemented KodeZwerg's code and it compiles without problem. :)

 

TinyPortal © 2005-2018