Jump to content

Merge two 'while'


telmob
 Share

Recommended Posts

The restriction policies are actually the easy part of the script. I actually took that part from here in the forums, i'm just 'enhancing' it. I want a proper UI and easy configuration.

The timer showing the time left until lock policy is in place is whats really annoying me....

Thank you for your support.

Link to comment
Share on other sites

You need to approach this logically. The while loop is where you call the timer, and there is nothing in there to alter that. I have modified the code to stop counting down at 1 second and it no longer exits automatically. Instead the script continues to loop forever (stuck on 1 second). Make sure you run it in SciTE so you can easily quit the running script. You need to add the correct arguments inside your loop and the timer function, to make it behave as you intend.

#include <WindowsConstants.au3>

Global $bInterrupt = True
Global $initimeout=iniread("SRPE.ini", "General", "UnlockTimeout", .2)*60*1000
Global $_CompteArebour = $initimeout, $_Minutes, $_Seconds

$_GuiCountDown = GUICreate ( "CountDown...", 500, 200, @DesktopWidth/2 -250, @DesktopHeight/2 -100, $WS_EX_TOPMOST )
$Time2Lock = GUICtrlCreateLabel("to auto-lock", 176, 16, 60, 17)
$Time2Lock = GUICtrlCreateLabel("00:00", 144, 16, 31, 17)
GUICtrlSetColor(-1, 0xFF0000)
GUISetState ( )
WinSetOnTop ( $_GuiCountDown, "", 1 )
$TimeTicks = TimerInit ( )

While 1
    ; Read ini
    If $bInterrupt Then _Check ( )
    Sleep ( 200 )
    ; read ini and set $bInterrupt flag according to your needs
WEnd

Func _Check ( )
    $_CompteArebour -= TimerDiff ( $TimeTicks )
    $TimeTicks = TimerInit ( )
    Local $_MinCalc = Int ( $_CompteArebour / ( 60 * 1000 ) ), $_SecCalc = $_CompteArebour - ( $_MinCalc * 60 * 1000 )
    $_SecCalc = Int ( $_SecCalc / 1000 )
    If $_MinCalc <= 0 And $_SecCalc <= 0 Then
        $bInterrupt = False
        ;GUICtrlSetData ( $Time2Lock, "Bye !" )
        ;Sleep ( 1000 )
        ;Exit
    Else
        If $_MinCalc <> $_Minutes Or $_SecCalc <> $_Seconds Then
            $_Minutes = $_MinCalc
            $_Seconds = $_SecCalc
            GUICtrlSetData ( $Time2Lock, StringFormat ( "%02u" & ":" & "%02u", $_Minutes, $_Seconds ) )
        EndIf
    EndIf
EndFunc

Anyway I stopped it counting down. So here is a possible method which you may wish to adapt.

Edited by czardas
Link to comment
Share on other sites

@czardas,

Dammit! You beat me by 12 minutes...

@telmob - another possible solution...kind of hokey but got rid of the timer calcs

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <timers.au3>

#AutoIt3Wrapper_Add_Constants=n

Global $timeout = IniRead("SRPE.ini", "General", "UnlockTimeout", "")

$_GuiCountDown  =     GUICreate("CountDown...", 500, 200, @DesktopWidth / 2 - 250, @DesktopHeight / 2 - 100, $WS_EX_TOPMOST)
$Time2Locklabel =     GUICtrlCreateLabel("Auto Lock", 200,30, 180, 50)
                    guictrlsetfont(-1,14,800,2)
                    guictrlsetcolor(-1,0xff)
                    guictrlcreatelabel('Time Left',210,60,90,20)
                    guictrlcreatelabel('Status',210,80,50,20)
$Time2Lockvalue =     GUICtrlCreateLabel("00:00", 270, 60, 35, 20)
                    GUICtrlSetColor(-1, 0xFF0000)
                    guictrlsetfont(-1,8.5,800)
$timelockstate  =   guictrlcreatelabel('Uninitialized',270,80,80,20)
                    guictrlsetstate(-1,$gui_unchecked)
                    GUICtrlSetColor(-1, 0xaa0000)
                    guictrlsetfont(-1,8.5,800)
$timelockchk    =     GUICtrlCreateCheckbox('Enable Auto-Lock',210,100,150,20)
                    GUISetState()

WinSetOnTop($_GuiCountDown, "", 1)

global $msg, $timeout

While 1

    $msg = guigetmsg()

    switch $msg
        case $gui_event_close
            exit
        case $timelockchk
            if  BitAnd(GUICtrlRead($timelockchk),$GUI_CHECKED) then
                guictrlsetdata($timelockstate,'Started')
                 adlibregister("_UpdateTime",1000)
            else
                guictrlsetdata($timelockstate,'Stopped')
                $timeout = IniRead("SRPE.ini", "General", "UnlockTimeout", "")
                adlibunregister('_UpdateTime')
            EndIf
    endswitch

WEnd

Func _UpdateTime()
    $timeout -= 1
    if $timeout < 1 then
        GUICtrlSetData($Time2Lockvalue, "Bye !")
        GUICtrlSetData($timelockstate, "Stopped")
        guictrlsetstate($timelockchk,$gui_unchecked)
        adlibunregister('_UpdateTime')
        $timeout = IniRead("SRPE.ini", "General", "UnlockTimeout", "")
    else
        GUICtrlSetData($Time2Lockvalue, StringFormat("%02u" & ":" & "%02u", mod($timeout/60,60), mod($timeout, 60 )))
    EndIf
EndFunc   ;==>_Check

kylomas

edit: code correction to refresh time out value at time interval expiration

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Ok, i think i haven't explained very well.

Please try this portion of code i managed to cut for example:

You will see when you check 'Enable unlock timeout', the countdown starts, and after you uncheck it, the countdown stops. But it doesn't reset.

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $inifile=iniread(@ScriptDir & "SRPE.ini", "General", "UnlockTimeout", "")
Global $bInterrupt = True
Global $initimeout=iniread($inifile, "General", "UnlockTimeout", .2)*60*1000
Global $_CompteArebour = $initimeout, $_Minutes, $_Seconds

$SRPEGUI = GUICreate("SRP Enforcer", 576, 408, 365, 236, $WS_SYSMENU)
$CheckBoxEnableTimeout = GUICtrlCreateCheckbox("Enable unlock timeout", 24, 64, 137, 17)
GUICtrlSetTip(-1, "Set system to locked mode automatically after a period of time")
$LabelMinutesRemain = GUICtrlCreateLabel("Minutes to remain in unlocked mode:", 40, 96, 177, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
$ComboMinutesRemain = GUICtrlCreateCombo("", 224, 93, 41, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData($ComboMinutesRemain,"10|20|30|40|50|60",30)
GUICtrlSetState(-1, $GUI_DISABLE)
$Time2Lock2 = GUICtrlCreateLabel("to auto-lock", 176, 16, 60, 17)
GUICtrlSetState(-1, $GUI_HIDE)
$Time2Lock = GUICtrlCreateLabel("00:00", 144, 16, 31, 17)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetState(-1, $GUI_HIDE)
GUISetState(@SW_SHOW)

$TimeTicks = TimerInit ( )

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $CheckBoxEnableTimeout
If GUICtrlRead($CheckBoxEnableTimeout)=$GUI_CHECKED Then
GUICtrlSetState($LabelMinutesRemain, $GUI_ENABLE)
GUICtrlSetState($ComboMinutesRemain, $GUI_ENABLE)
Iniwrite($inifile, "General", "UnlockTimeout", "30")
$bInterrupt = True
EndIf
If GUICtrlRead($CheckBoxEnableTimeout)=$GUI_UNCHECKED Then
GUICtrlSetState($LabelMinutesRemain, $GUI_DISABLE)
GUICtrlSetState($ComboMinutesRemain, $GUI_DISABLE)
Iniwrite($inifile, "General", "UnlockTimeout", "0")
$bInterrupt = False
EndIf
Case $ComboMinutesRemain
EndSwitch

Sleep(50)
If IniRead($inifile, "General", "UnlockTimeout", "") > 0 And GUICtrlRead($CheckBoxEnableTimeout)=$GUI_CHECKED Then
GUICtrlSetState($Time2Lock, $GUI_SHOW)
GUICtrlSetState($Time2Lock2, $GUI_SHOW)
$bInterrupt = True
_Check ( )
EndIf
If IniRead($inifile, "General", "UnlockTimeout", "") = 0 And GUICtrlRead($CheckBoxEnableTimeout)=$GUI_UNCHECKED Then
GUICtrlSetState($Time2Lock, $GUI_HIDE)
GUICtrlSetState($Time2Lock2, $GUI_HIDE)
$initimeout=0
$bInterrupt = False
EndIf
WEnd

Func _Check ( )
$_CompteArebour -= TimerDiff ( $TimeTicks )
$TimeTicks = TimerInit ( )
Local $_MinCalc = Int ( $_CompteArebour / ( 60 * 1000 ) ), $_SecCalc = $_CompteArebour - ( $_MinCalc * 60 * 1000 )
$_SecCalc = Int ( $_SecCalc / 1000 )
If $_MinCalc <= 0 And $_SecCalc <= 0 Then
$bInterrupt = False
;GUICtrlSetData ( $Time2Lock, "Bye !" )
;Sleep ( 1000 )
;Exit
Else
If $_MinCalc <> $_Minutes Or $_SecCalc <> $_Seconds Then
$_Minutes = $_MinCalc
$_Seconds = $_SecCalc
GUICtrlSetData ( $Time2Lock, StringFormat ( "%02u" & ":" & "%02u", $_Minutes, $_Seconds ) )
EndIf
EndIf
EndFunc

The SRPE.ini file contents:

[General]

UnlockTimeout=30

Edited by telmob
Link to comment
Share on other sites

  • Developers

This looks wrong:

Global $inifile = IniRead(@ScriptDir & "SRPE.ini", "General", "UnlockTimeout", "")

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

So, did you ask yourself the question where in the script you are setting control $CheckBoxEnableTimeout back to the INI value?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Also notice that I made a change to this line in my earlier post. This was to try to understand what you were doing (using a 10 second countdown). I think you overlooked that I had modified it.

Global $initimeout = iniread($inifile, "General", "UnlockTimeout", "")*60*1000
Edited by czardas
Link to comment
Share on other sites

So, did you ask yourself the question where in the script you are setting control $CheckBoxEnableTimeout back to the INI value?

Yes, in my main script (the one i posted is just a tiny part) the combo writes que value selected to the ini file.

Also notice that I made a change to this line in my earlier post. This was to try to understand what you were doing (using a 10 second countdown). I think you overlooked that I had modified it.

Global $initimeout = iniread($inifile, "General", "UnlockTimeout", "")*60*1000
No, I saw that. I was wondering how could '.2' mean 10 minutes :) but i don't think my problem is in any related to that.
Link to comment
Share on other sites

Yes, in my main script (the one i posted is just a tiny part) the combo writes que value selected to the ini file.

On the one hand your script has multiple issues. Please try to identify one issue at a time and try to post code which demonstrates that particular issue. If the problem happens to be in the part of the code you didn't post, we can never solve it.

It's also not a good idea to post 1000 lines of code when you only wish to ask a simple question which can be demonstrated in just 10 lines. When modifying someone else's code, try to (make sure you) understand exactly what every line in the code does. Anyway, it's good you are attempting that, and this is general advice for anyone really.

No, I saw that. I was wondering how could '.2' mean 10 minutes :) but i don't think my problem is in any related to that.

Oops, me too (lol)

0.2 * 60 * 1000 = 12000 ms

12000 ms = 12 seconds

Link to comment
Share on other sites

  • Developers

Yes, in my main script (the one i posted is just a tiny part) the combo writes que value selected to the ini file.

The question was not when you write it to the INI but when you update the GUI control with the value in the INI (Given you set the $inifile also correctkly first!) ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

First of all, sorry for taking so long to reply but i have been very busy at work, and yet, very obcessed with this..

So i finally managed to solve it, using an AdlibRegister example.

Here's what i did:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $inifile=@ScriptDir & "\SRPE.ini"
Global $seconds = 1*60

$SRPEGUI = GUICreate("SRP Enforcer", 576, 408, 365, 236, $WS_SYSMENU)
$CheckBoxEnableTimeout = GUICtrlCreateCheckbox("Enable unlock timeout", 24, 64, 137, 17)
GUICtrlSetTip(-1, "Set system to locked mode automatically after a period of time")
$LabelMinutesRemain = GUICtrlCreateLabel("Minutes to remain in unlocked mode:", 40, 96, 177, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
$ComboMinutesRemain = GUICtrlCreateCombo("", 224, 93, 41, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData($ComboMinutesRemain,"10|20|30|40|50|60",30)
GUICtrlSetState(-1, $GUI_DISABLE)
$Time2Lock2 = GUICtrlCreateLabel("to auto-lock", 176, 16, 60, 17)
GUICtrlSetState(-1, $GUI_HIDE)
$Time2Lock = GUICtrlCreateLabel("00:00", 144, 16, 31, 17)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetState(-1, $GUI_HIDE)
GUISetState(@SW_SHOW)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $CheckBoxEnableTimeout
If GUICtrlRead($CheckBoxEnableTimeout)=$GUI_CHECKED Then
GUICtrlSetState($LabelMinutesRemain, $GUI_ENABLE)
GUICtrlSetState($ComboMinutesRemain, $GUI_ENABLE)
Iniwrite($inifile, "General", "UnlockTimeout", "1")
$seconds = iniread($inifile, "General", "UnlockTimeout", "")*60
_Check ( )
AdlibRegister("_Check", 1000)
EndIf
If GUICtrlRead($CheckBoxEnableTimeout)=$GUI_UNCHECKED Then
AdlibUnRegister("_Check")
$seconds = 0
GUICtrlSetState($LabelMinutesRemain, $GUI_DISABLE)
GUICtrlSetState($ComboMinutesRemain, $GUI_DISABLE)
Iniwrite($inifile, "General", "UnlockTimeout", "0")
EndIf
Case $ComboMinutesRemain
EndSwitch

Sleep(50)
If IniRead($inifile, "General", "UnlockTimeout", "") > 0 And GUICtrlRead($CheckBoxEnableTimeout)=$GUI_CHECKED Then
GUICtrlSetState($Time2Lock, $GUI_SHOW)
GUICtrlSetState($Time2Lock2, $GUI_SHOW)
EndIf
If IniRead($inifile, "General", "UnlockTimeout", "") = 0 And GUICtrlRead($CheckBoxEnableTimeout)=$GUI_UNCHECKED Then
GUICtrlSetState($Time2Lock, $GUI_HIDE)
GUICtrlSetState($Time2Lock2, $GUI_HIDE)
$initimeout=0
EndIf
WEnd

AdlibUnRegister("_Check")

Func _Check ( )
Local $sec, $min
$sec = Mod($seconds, 60)
$min = Mod($seconds / 60, 60)
GUICtrlSetData($Time2Lock, StringFormat("%02i:%02i", $min, $sec))
If $seconds <= 0 Then
AdlibUnRegister("_Check")
MsgBox(0, "Information", "Countdown reached 00:00:00")
Exit
EndIf
$seconds -= 1EndFunc

Thank you so much for your help and support.

I noticed i was hacing problems with the placements of AdlibRegister and function calls.

Now i understand how AdlibRegister works and its pretty simple to implement. I am now a happy man :D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...