Timeout to close App

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

edk
Posts: 954
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Timeout to close App

Post by edk »

Is that your goal?:

Code: Select all

#include "hmg.ch"
#include "i_MsgBox.ch"

*************
FUNCTION Main
*************
Local nIdleTimeToClose := 10

define window sample at 0,0 width 640 height 480 ;
   title "System Idle Time " + StrZero ( SysIdleSecs(), 4 )


  @12, 10 LABEL Label_1 VALUE "Text_1" width 80 RIGHTALIGN

  @10, 95 TEXTBOX Text_1 ;
    VALUE 10 ;
    NUMERIC

  @42, 10 LABEL Label_2 VALUE "Text_2" width 80 RIGHTALIGN

  @40, 95 TEXTBOX Text_2 ;
    VALUE 20 ;
    NUMERIC

  @72, 10 LABEL Label_3 VALUE "Text_3" width 80 RIGHTALIGN

  @70, 95 TEXTBOX Text_3 ;
    VALUE 30 ;
    NUMERIC

  DEFINE TIMER IdleTimeOut INTERVAL 1000 ACTION CheckIdleTimeout( nIdleTimeToClose )

end window
sample.Text_1.setfocus
center window sample
activate window sample

RETURN Nil

*********************************************************************
Procedure CheckIdleTimeout( nIdleTimeOut )
Local nSysIdleTime

Static nIdleTimeOutLevel := 0
Static nNextIdleTimeOut  := 0

This.Enabled := .F.     //disable timer 
nSysIdleTime := SysIdleSecs()

IF nSysIdleTime < nIdleTimeOut .AND. ( nIdleTimeOutLevel <> 0 .OR. nNextIdleTimeOut = 0 )	//System idle time is less than timeout, it is level 0
     IF nIdleTimeOutLevel > 0
          This.Value := 1000   //set the timer back to 1 second    
          ScreenSaver( /* cInfo */ , .T. /* lRelease */ )
     ENDIF
     nIdleTimeOutLevel := 0 
     nNextIdleTimeOut := nIdleTimeOut
ENDIF

IF nSysIdleTime >= nNextIdleTimeOut   //System idle time has reached timeout, level up, increasing the timeout idle time to the next level.
     nIdleTimeOutLevel ++
     nNextIdleTimeOut := nSysIdleTime + 60     //nSysIdleTime + nIdleTimeOut
ENDIF
   
DO CASE
     CASE nIdleTimeOutLevel = 2
         MessageBoxTimeout ( "The system idle timeout level " + StrZero ( nIdleTimeOutLevel, 1 )  + " has been reached, the application will be closed in 5 seconds.", "Idle state", MB_ICONWARNING + MB_TOPMOST, 5 * 1000 )
         ReleaseAllWindows()
     CASE nIdleTimeOutLevel = 1       
         This.Value := 50     //set the timer to 50 milliseconds, refresh screen saver faster. 
         ScreenSaver( "System idle timeout reached. The app will close in " +  StrZero ( nNextIdleTimeOut - nSysIdleTime, 2 ) + " sec." )
ENDCASE


ThisWindow.Title := "System Idle Time " + StrZero ( nSysIdleTime, 4 )
This.Enabled := .T.   //enable timer

RETURN Nil

*******************************************************************
Procedure ScreenSaver( cInfo, lRelease )

Local nColLabel := GetDesktopWidth() / 2
Local nRowLabel := GetDesktopHeight() / 2
Local nFontHeight := 28

Static nDirectionX := 0
Static nDirectionY := 0

Default lRelease := .F.
Default cInfo := "Screen Saver"

IF .Not. IsWindowActive ( _ScreenSaver_ ) .AND. .Not. lRelease
     DEFINE WINDOW _ScreenSaver_ AT 0, 0 WIDTH GetDesktopWidth() HEIGHT GetDesktopHeight() ;
            TOPMOST NOMINIMIZE NOMAXIMIZE NOSIZE NOSYSMENU NOCAPTION ;
            BACKCOLOR BLACK
     END WINDOW

     @nRowLabel, nColLabel LABEL Label_1 OF _ScreenSaver_  VALUE cInfo AUTOSIZE ;
                        FONT "Arial Rounded MT" SIZE nFontHeight BOLD ;
                        FONTCOLOR WHITE TRANSPARENT

     //Center Label
     _ScreenSaver_.Label_1.Col := _ScreenSaver_.Label_1.Col - ( _ScreenSaver_.Label_1.Width / 2 )

     nDirectionX := hb_RandomInt ( 0, 2 )
     nDirectionY := hb_RandomInt ( 0, 2 )

     IF nDirectionX = 2
          nDirectionX := -1
     ENDIF

     IF nDirectionY = 2
          nDirectionY := -1
     ENDIF

     ACTIVATE WINDOW _ScreenSaver_

ELSE

     IF lRelease
          _ScreenSaver_.Release
     ELSE
   
          _ScreenSaver_.Label_1.Value     := cInfo
          _ScreenSaver_.Label_1.Col       := _ScreenSaver_.Label_1.Col + ( nDirectionX * 3 )
          _ScreenSaver_.Label_1.Row       := _ScreenSaver_.Label_1.Row + ( nDirectionY * 3 )
          _ScreenSaver_.Label_1.FontColor := { hb_RandomInt ( 0, 255 ), hb_RandomInt ( 0, 255 ), hb_RandomInt ( 0, 255 ) }

          IF _ScreenSaver_.Label_1.Col <= 0 .OR. ( _ScreenSaver_.Label_1.Col + _ScreenSaver_.Label_1.Width ) >= _ScreenSaver_.Width
               nDirectionX := nDirectionX * ( -1 )          //bounce   
          ENDIF

          IF _ScreenSaver_.Label_1.Row <= 0 .OR. ( _ScreenSaver_.Label_1.Row + nFontHeight + IF( nFontHeight < 12, 12, 16 )  ) >= _ScreenSaver_.Height
               nDirectionY := nDirectionY * ( -1 )          //bounce   
          ENDIF
  
    ENDIF

ENDIF

RETURN Nil

*******************************************************************************
#pragma BEGINDUMP

#include "windows.h"
#include "time.h"
#include "hbapi.h"

HB_FUNC( SYSIDLESECS ) 
{
 LASTINPUTINFO lpi;
 lpi.cbSize = sizeof (LASTINPUTINFO);
 GetLastInputInfo (&lpi);
 hb_retnd( ( DOUBLE ) ( GetTickCount() - lpi.dwTime ) / CLOCKS_PER_SEC ); 
}

#pragma ENDDUMP
Last edited by edk on Sun Aug 11, 2024 7:54 pm, edited 1 time in total.
User avatar
AUGE_OHR
Posts: 2083
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: Timeout to close App

Post by AUGE_OHR »

hi Edward,
edk wrote: Fri Aug 09, 2024 7:50 am Is that your goal?:
YES, THX

now it is working like i want, but i do´nt understand how ?

at Level 0 it Count UP as expect
at Level 1 it Count DOWN, but i see no SUBTRACT in Code :shock:
have fun
Jimmy
edk
Posts: 954
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Timeout to close App

Post by edk »

There is no level subtraction because when the system is no longer idle it returns to level 0. :mrgreen:

Code: Select all

IF nSysIdleTime < nIdleTimeOut .AND. ( nIdleTimeOutLevel <> 0 .OR. nNextIdleTimeOut = 0 )	//System idle time is less than timeout, it is level 0
     IF nIdleTimeOutLevel > 0
          This.Value := 1000   //set the timer back to 1 second    
          ScreenSaver( /* cInfo */ , .T. /* lRelease */ )
     ENDIF
     nIdleTimeOutLevel := 0 
     nNextIdleTimeOut := nIdleTimeOut
ENDIF
Level 0: Idle timeout not exceeded.

Level 1: first idle threshold exceeded, execute screen saver, if during level 1 the system is no longer idle, reverts to level 0.

Level 2: second final idle threshold exceeded, terminate application.
Post Reply