Jump to content

Strange issue with Adlib (and timers)


 Share

Recommended Posts

I have a 'tick once per second' timer created with AdlibRegister(). If the user clicks a button it calls a function that does a lot, and while running the 'once per second' timer updates the elapsed time, second by second.

If a checkbox is checked then each 20 seconds the same function is called. While that function runs, the elapsed time display does NOT update. It appears the function just does not run at all.

Below is a little demo program to show this behaviour. If you click 'Waste time' you'll see that the ConsoleWrite()'s continue. If you check the checkbox in 20 seconds "Time wasting started" will be displayed and the elapsed time display stops until the called function WasteTime() is complete.

I've spent 12 hours on this to no effect. I've re-written it twice, the 2nd time using _Timer_SetTimer() but it did the same thing so I suspect the Adlib functions and the timer functions share code. I really hope somebody can advise coz I'm very close to losing my sh*t!

#include <GUIConstantsEx.au3>
Global $intCount = 20, $intElapsedSeconds, $btnWasteTime, $guiTest, $chkTest, $intTimeout
;*****************************
$intElapsedSeconds = 0
$intTimeout = 20
BuildGUI()
AdlibRegister("CountSeconds", 1000)      ; Start the once per second counter
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $btnWasteTime
   WasteTime()
EndSwitch
WEnd
;*****************************
Func CountSeconds()
$intElapsedSeconds += 1        ; Bump number of elapsed seconds
MyConsoleWrite("$intElapsedSeconds = " & $intElapsedSeconds)
If GUICtrlRead($chkTest) = $GUI_CHECKED Then
  $intTimeout -= 1
  If $intTimeout = 0 Then
   WasteTime()
   $intTimeout = 20
   $intElapsedSeconds = 0
  EndIf
EndIf
If $intElapsedSeconds = 20 Then $intElapsedSeconds = 0
EndFunc  ; End of Func CountSeconds()
;*****************************
Func WasteTime()
Local $Counter1, $Counter2
MyConsoleWrite("Time wasting started")
For $Counter1 = 1 To 10000000        ; This takes about 12 seconds on my pc. Adjust $Counter1 For/Next loop for something < 20 seconds.
  $Counter2 = 2 * $Counter1
Next
MyConsoleWrite("Time wasting complete")
EndFunc  ; End of Func WasteTime()
;*****************************
Func MyConsoleWrite($String)
ConsoleWrite($String & @CRLF)
EndFunc  ; End of Func MyConsoleWrite()
;*****************************
Func BuildGUI()
$guiTest = GUICreate("Test", 500, 365, -1, -1)
$chkTest = GUICtrlCreateCheckbox("Test", 100, 100, 250, 20)
$btnWasteTime = GUICtrlCreateButton("WasteTime", 100, 200, 100, 25)
GUISetState(@SW_SHOW)
EndFunc  ; End of Func BuildGUI()
;*****************************
Link to comment
Share on other sites

  • Moderators

4Eyes,

When you tick the checkbox, you are running your WasteTime function within the Adlib function which therefore does not end before it is called again several times over. Once you get into overlapping Adlib functions, all bets are off as to what the script behaviour will be - and I have no idea what you are trying to do with that convoluted code inside the If structure you have within the function. :oops:

Can you explain what you want to happen when the checkbox is ticked and how it should differ from the button being pressed (which seems to work correctly). :bye:

M23

P.S. If you want an exact delay, use TimerInit/TimeDiff like this:

Do
    Sleep(10)
Until TimerDiff($iBegin) > 12000

Much more accurate than a For...Next loop

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Melba23,

Thanks for your reply. Yep, the example is ugly, but it simulates what I see with the real program. Ahh... I don't see that WasteTime() is called multiple times. If that was the case we'd see multiple "Time wasting started" messages.

I'll PM you more details. If this gets resolved and appears useful to other folks then I'm more than happy to share the result.

Link to comment
Share on other sites

  • Moderators

4Eyes,

I'll PM you more details

Why not post them here directly? :oops:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

M23,

Well, if you have time to read the PM (no offence intended) and consider that I should make it public then I will. The project is not necessarily for public domain, nor is it a game bot. BTW, I am not trying to monopolize your time either.

4Eyes

Link to comment
Share on other sites

  • Moderators

4Eyes,

That was interesting. I tried to register another Adlib function within a running Adlib function to get a duration timer - and it did not work. So I have had to go for this rather convoluted approach: :bye:

#include <GUIConstantsEx.au3>

Global $iAudit_Timer

$hGUI = GUICreate("Test", 500, 500)

; Run an audit when you want
$cButton = GUICtrlCreateButton("Run now", 10, 10, 80, 30)

; Run an audit every 20 secs
$cCheckBox = GUICtrlCreateCheckBox(" Run Auto", 10, 100, 200, 20)

; We need this - trust me!
$cDummy = GUICtrlCreateDummy()

GUISetState()

; This is just to show the passing time - you can delete it and its function
AdlibRegister("_Timer", 1000)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            ; The button was pressed
            ConsoleWrite("Audit started by button" & @CRLF)
            ; Prevent the auto audit from starting while this audit is active
            AdlibUnRegister("_Audit_Starter")
            ; And now run the audit using the code in the next Case
            ContinueCase
        Case $cDummy
            ; The audit was started automatically - or we are here after a button press
            ; Disable to button to prevent multiple audits
            GUICtrlSetState($cButton, $GUI_DISABLE)
            ; Start the audit duration timer
            $iAudit_Timer = 0
            AdlibRegister("_Audit_Timer", 1000)
            ; Run teh audit
            _Audit()
            ; Now stop the audit duration timer
            AdlibUnRegister("_Audit_Timer")
            ; Restart the auto audit timer
            AdlibRegister("_Audit_Starter", 20000)
            ; Reactivate the button
            GUICtrlSetState($cButton, $GUI_ENABLE)
        Case $cCheckBox
            ; Activate or disable the auto audit timer
            Switch GUICtrlRead($cCheckBox)
                Case 1
                    AdlibRegister("_Audit_Starter", 20000)
                Case Else
                    AdlibUnRegister("_Audit_Starter")
            EndSwitch
    EndSwitch

WEnd

Func _Audit_Starter()

    ; Start the audit automatically
    ConsoleWrite("Audit started automatically" & @CRLF)
    ; By firing the dummy control
    GUICtrlSendToDummy($cDummy)

EndFunc

Func _Audit()

    ; Simulate audit
    ConsoleWrite("Audit Started" & @CRLF)

    ; I am presuming we cannot break in here which is why we need the Adlib duration timer
    $iBegin = TimerInit()
    Do
        Sleep(10)
    Until TimerDiff($iBegin) > 5000

    ConsoleWrite("Audit Stopped" & @CRLF)

EndFunc

Func _Audit_Timer()

    ; This runs during the audit to give an elapsed time
    $iAudit_Timer += 1
    ConsoleWrite("Audit: " & $iAudit_Timer & @CRLF)

EndFunc

; This is just to show a running timer and can be deleted
Func _Timer()

    ConsoleWrite(@SEC & @CRLF)

EndFunc

I hope the comments are clear enough - please ask if not. :oops:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Melba23,

Ok... I'll look at this closely tommorrow as it's getting late and my brain is long gone. Yes, yours works, now I have to work out why, and why my original doesn't. I'm not going to let so much effort go without getting something back. Grrr! :oops:

I originally had 2 Adlib driven timers and had no end of sillyness... it appeared that canning one actually canned the other etc. Just lots of sillyness.

As I mentioned, I tried using a timer too, but had no success.

Thanks for your valuable input, as always.

4Eyes

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...