Recent

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

Davidous

  • Full Member
  • ***
  • Posts: 107
Prevent computer logging out
« on: February 02, 2023, 07:05:51 pm »
Hello!

I am looking for a way to prevent computer from logging out. It is because I work on a company laptop and I made a program that generates a very long protocol and it takes time to finish. Until it finishes the computer must not log out. (Since this is a company laptop, automatic logging out after 15 min is a default setting and can not be changed.)  Right now the only way to prevent it is to move the mouse from time to time but for that I need to keep that in mind and do it again and again.
Does anyone know a way to make this "preventing logging out" automatically? I read something about "SetThreadExecutionState" but I don't now the proper way to use it and couldn't find it on google.

af0815

  • Hero Member
  • *****
  • Posts: 1334
Re: Prevent computer logging out
« Reply #1 on: February 02, 2023, 07:19:20 pm »
Long time ago there were some programms, they simulate a mousemove to prevent the computer to be sleeping.

Google: windows app no sleeping pc

one of some information:
https://learn.microsoft.com/en-us/windows/powertoys/awake
https://windowsreport.com/prevent-computer-from-sleeping-locking/
regards
Andreas

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Prevent computer logging out
« Reply #2 on: February 02, 2023, 07:20:28 pm »
Since you not mention for what kind of OS, for Windows I can provide a solution if needed.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 15139
  • Censorship about opinions does not belong here.
Re: Prevent computer logging out
« Reply #3 on: February 02, 2023, 07:25:40 pm »
Me too and also for nixes.
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

Davidous

  • Full Member
  • ***
  • Posts: 107
Re: Prevent computer logging out
« Reply #4 on: February 02, 2023, 07:37:56 pm »
I need it for Windows. I would be very grateful for that! :D

balazsszekely

  • Guest
Re: Prevent computer logging out
« Reply #5 on: February 02, 2023, 07:56:35 pm »
I need it for Windows. I would be very grateful for that! :D
Please check attached project. The program will move the mouse after the system is idle for x seconds.

af0815

  • Hero Member
  • *****
  • Posts: 1334
Re: Prevent computer logging out
« Reply #6 on: February 02, 2023, 07:56:58 pm »
He said "company laptop". I have never seen another System like Windows in copanies with central IT.


@GemMem: i know this kind of software in pascal since windows have a screensaver :-) Good Shoot
« Last Edit: February 02, 2023, 08:00:19 pm by af0815 »
regards
Andreas

dseligo

  • Hero Member
  • *****
  • Posts: 1282
Re: Prevent computer logging out
« Reply #7 on: February 02, 2023, 08:15:47 pm »
Please check attached project. The program will move the mouse after the system is idle for x seconds.

Line 52 should be like below if you want to set idle time with spinedit (you left hard coded 9 in that line):
Code: Pascal  [Select][+][-]
  1.   if IdleTime > spSec.Value then

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Prevent computer logging out
« Reply #8 on: February 02, 2023, 08:18:18 pm »
Hello!

I am looking for a way to prevent computer from logging out. It is because I work on a company laptop and I made a program that generates a very long protocol and it takes time to finish. Until it finishes the computer must not log out. (Since this is a company laptop, automatic logging out after 15 min is a default setting and can not be changed.)  Right now the only way to prevent it is to move the mouse from time to time but for that I need to keep that in mind and do it again and again.
Does anyone know a way to make this "preventing logging out" automatically? I read something about "SetThreadExecutionState" but I don't now the proper way to use it and couldn't find it on google.
You need the SetThreadExecutionState like this:
Code: Pascal  [Select][+][-]
  1. const
  2.  ES_CONTINUOUS       = $80000000;
  3.  ES_DISPLAY_REQUIRED = $00000002;
  4.  ES_SYSTEM_REQUIRED  = $00000001;
  5.  
  6. function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';
Call it in a Timer to preserve system from those flags.
I would call those by interval
Code: Pascal  [Select][+][-]
  1.   SetThreadExecutionState( ES_SYSTEM_REQUIRED );
  2.   SetThreadExecutionState( ES_DISPLAY_REQUIRED );
Also you will need to disable ScreenSaver, either by hand or by code to patch this HKEY:
Code: Pascal  [Select][+][-]
  1. HKEY_CURRENT_USER -> '\Control Panel\Desktop' -> 'ScreenSaveActive' -> '0'
I hope you understand

Any questions left?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Davidous

  • Full Member
  • ***
  • Posts: 107
Re: Prevent computer logging out
« Reply #9 on: February 02, 2023, 08:23:13 pm »
Thank you very much! No questions yet. I will try it tomorrow! :)

balazsszekely

  • Guest
Re: Prevent computer logging out
« Reply #10 on: February 02, 2023, 08:23:48 pm »
@GemMem: i know this kind of software in pascal since windows have a screensaver :-) Good Shoot
Thanks. I'm using it for years, it works fine.


Please check attached project. The program will move the mouse after the system is idle for x seconds.

Line 52 should be like below if you want to set idle time with spinedit (you left hard coded 9 in that line):
Code: Pascal  [Select][+][-]
  1.   if IdleTime > spSec.Value then
That was the easter egg.  :D


« Last Edit: February 02, 2023, 08:28:39 pm by GetMem »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Prevent computer logging out
« Reply #11 on: February 02, 2023, 08:34:02 pm »
Since Win2k/WinXP I do used my shown way, tricky part was registry.
I programmed it for a media player, moving mouse would start show timeline so that was a no-go in my case.

For registry I can recommend, on startup of your app, collect the info from registry, it is a string, replace that string with really a "0" as string, when ending app, restore your original saved value.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Davidous

  • Full Member
  • ***
  • Posts: 107
Re: Prevent computer logging out
« Reply #12 on: February 02, 2023, 08:46:08 pm »
For the

function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';

I got the error message "cannot declare local procedure as EXTERNAL"
Do you know why this occures?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Prevent computer logging out
« Reply #13 on: February 02, 2023, 08:48:05 pm »
Do you know why this occures?
When you show your source I can tell whats wrong.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Davidous

  • Full Member
  • ***
  • Posts: 107
Re: Prevent computer logging out
« Reply #14 on: February 02, 2023, 09:02:37 pm »
My code starts with many constants, after it the procedure begins with "begin" but it is very long. I just got the error with that one line with the "SetThreadExecutionState". If I delete it, the program compiles without any complications. Is there something extra neccessary in the "Uses" part for it to work?

#procedure TForm1.Button13Click(Sender: TObject);

const
  xlBottom = -4107;
  xlTop = -4160;
  xlLeft = -4131;
  xlRight = -4152;
  xlHAlignCenter = -4108;
  xlVAlignCenter = -4108;
  xlContinuous = 1;
  xlThin = 2;
  xlEdgeBottom = 9;
  xlEdgeLeft = 7;
  xlEdgeRight = 10;
  xlEdgeTop = 8;
  xlInsideHorizontal = 12;
  xlInsideVertical = 11;
  xlDown = -4121;
  xlToLeft = -4159;
  xlToRight = -4161;
  xlUp = -4162;
  xlSortNormal = 0;
  xlAscending = 1;
  xlDescending = 2;
  xlSortValues = 1;
  xlPinYin = 1;
  xlToptoBottom = 1;
  xlPasteColumnWidths = 8;
  xlPasteFormats = -4122;
  xlPasteValues = -4163;
  xlPasteSpecialOperationNone = -4142;
  xlNone = -4142;
  xlPageBreakPreview = 2;
  xlFormulas = -4123;
  xlNext = 1;
  xlByRows = 1;
  xlWhole = 1;
  xlDelimited = 1;
  xlDoubleQuote = 1;
  xlOr = 2;
  xlPart = 2;
  xlPortrait = 1;
  xlLandscape = 2;
  xlTypePDF = 0;
  msoTrue=-1;
  xlMedium = -4138;
  xlEqual = 3;
  xlNotEqual = 4;
  xlCellValue = 1;

 ES_CONTINUOUS       = $80000000;
 ES_DISPLAY_REQUIRED = $00000002;
 ES_SYSTEM_REQUIRED  = $00000001;

function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';

begin#

 

TinyPortal © 2005-2018