iAmNewbe

How do you use a Timer?

33 posts in this topic

I have tried to set a timer to run a function in a couple of projects but I could never get it to work so I just end up using loops of some sort.  

This is an example of how I have been trying to get them to work but I can't get any result what am I doing wrong?
I haven't tested the code below I just wrote it off top of my head as example of what I have been trying to do.

Nothing happens, TimedActivated never gets called.  In theory it should be called once a second and then when Case 3 is called inside that function the timer should be killed..  Notta..  Why?

 

#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Timers.au3>


Global $hGUI = GUICreate("", 400, 320)

startSomething()

 Func startSomething()
    Dim $hGui
    Global $myTimer = _Timer_SetTimer($hGUI, 1000, "TimedActivated") ; create timer
 EndFunc

 Func TimedActivated()
    Dim $myTimer
    $choice = Random(1,3,1)
    Switch $choice
        Case 1
            ; Code here does something
        Case 2
            ; Code here does something
        Case 3
            _Timer_KillTimer($hGUI, $myTimer)
            msgMe()
    EndSwitch
 EndFunc

 Func msgMe()
    MsgBox(0,'','Timer Activated')
 EndFunc

While 1
 ; Forever Loop
WEnd

 

Share this post


Link to post
Share on other sites



Add the callback parameters to your callback function:

TimedActivated($hWnd, $iMsg, $iIDTimer, $iTime)

From the help file: 

Remarks

The callback function is called with the following parameters:
$hWnd, $iMsg, $iIDTimer, $iTime


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

Ok, I changed this but once the MsgBox is called the application crashes

 

 

Func TimedActivated($hWnd, $iMsg, $iIDTimer, $iTime)
    Dim $hGui
    $choice = Random(1,3,1)
    Switch $choice
        Case 1
            ; Code here does something
        Case 2
            ; Code here does something
        Case 3
            _Timer_KillTimer($hGUI, $myTimer)
            msgMe()
    EndSwitch
 EndFunc

 

Share this post


Link to post
Share on other sites

Changed the program so it is more full fledged but every time Case 3 is called and after I close the "Timer Activated" msgbox the application crashes. 

 

#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Timers.au3>

Global $hGUI = GUICreate("", 400, 320)
GUISetState(@SW_SHOW)

startSomething()

 Func startSomething()
    Dim $hGui
    Global $myTimer = _Timer_SetTimer($hGUI, 1000, "TimedActivated") ; create timer
 EndFunc

 Func TimedActivated($hWnd, $iMsg, $iIDTimer, $iTime)
     #forceref $hWnd, $iMsg, $iIDTimer, $iTime
    Dim $hGui, $myTimer
    $choice = Random(1,3,1)
    Switch $choice
        Case 1
            MsgBox(0,'',"Case 1")
        Case 2
            MsgBox(0,'',"Case 2")
        Case 3
            _Timer_KillTimer($hGUI, $myTimer)
            msgMe()
    EndSwitch
 EndFunc

 Func msgMe()
    MsgBox(0,'','Timer Activated')
 EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd


 

Share this post


Link to post
Share on other sites

Try this and insert your own functions ... watch the Scite console to see it working ... 

#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Timers.au3>

Global $Form1 = GUICreate("", 400, 320)
$Form1 = GUICreate("Form1", 615, 437, 192, 124)
GUISetState(@SW_SHOW)

$myTimer = _Timer_SetTimer($Form1, 1000, "_TimedActivated") ; create timer


Func _TimedActivated($hWnd, $iMsg, $iIDTimer, $iTime)
    Dim $myTimer
    $choice = Random(1,3,1)
    ;ConsoleWrite($choice&@crlf)
    Switch $choice
        Case 1
            ConsoleWrite("case 1"&@crlf)
        Case 2
            ConsoleWrite("case 2"&@crlf)
        Case 3
            ConsoleWrite("case 3"&@crlf)
    EndSwitch
EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _Timer_KillAllTimers ($Form1)
            Exit
    EndSwitch
WEnd

 


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

Crashes after MsgBox is displayed.. Not sure why.

 

#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Timers.au3>

Global $Form1 = GUICreate("Form1", 615, 437, 192, 124)
GUISetState(@SW_SHOW)

Global $myTimer = _Timer_SetTimer($Form1, 1000, "_TimedActivated") ; create timer


Func _TimedActivated($hWnd, $iMsg, $iIDTimer, $iTime)
  Local $choice = Random(1,3,1)
    ;ConsoleWrite($choice&@crlf)
    Switch $choice
        Case 1
            ConsoleWrite("case 1"&@crlf)
        Case 2
            ConsoleWrite("case 2"&@crlf)
        Case 3
            _Timer_KillAllTimers ($Form1)
            MsgBox(0,'','Timer Activated')
            Exit
    EndSwitch
 EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _Timer_KillAllTimers ($Form1)
            Exit
    EndSwitch
WEnd

 

Share this post


Link to post
Share on other sites

The kill timer line whether it is _Timer_KillTimer or _Timer_KillAllTimers crash when called within the function and the timer is set somewhere else whether at begin of script or inside of another function.  Program Crash happens.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Hmmm...    For me it crashes, showing AutoIT has crashed and is no longer responsive.  I commented out the Exit and MsgBox and just left the timer kill line in case 3 and everytime that case is called the application crashes.   I am using Windows 10 Pro and the most recent stable build of AutoIT.

I did not compile the script I just run it directly from Scite using F5 key.

Edited by iAmNewbe

Share this post


Link to post
Share on other sites

#10 ·  Posted

I am also running Win10 (Home Edition) and AutoIt v3.3.12.0.  I also ran it from Scite.  Can you please post your exact code and the message you get when it crashes?


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

#11 ·  Posted

I am using the newest AutoIT version 3.3.14.2  and everytime it gets to case 3 and the kill timer code it crashes.

I commented out the display code on case 3 just to test if it wasn't that effecting anything it isn't but I didn't reactivate those lines yet.

Screen shot of error window that appears attached.

#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Timers.au3>

Global $Form1 = GUICreate("Form1", 615, 437, 192, 124)
GUISetState(@SW_SHOW)

Global $myTimer = _Timer_SetTimer($Form1, 1000, "_TimedActivated") ; create timer


Func _TimedActivated($hWnd, $iMsg, $iIDTimer, $iTime)
Dim $Form1, $myTimer
  Local $choice = Random(1,3,1)
    ;ConsoleWrite($choice&@crlf)
    Switch $choice
        Case 1
            ConsoleWrite("case 1"&@crlf)
        Case 2
            ConsoleWrite("case 2"&@crlf)
        Case 3
            _Timer_KillAllTimers ($Form1)
;~             ConsoleWrite("case 3"&@crlf)
;~             MsgBox(0,'','Timer Activated')
            Exit
    EndSwitch
 EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _Timer_KillAllTimers ($Form1)
            Exit
    EndSwitch
WEnd


 

error crash.jpg

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Had a thought.  Added  #AutoIt3Wrapper_UseX64=n  to the top of the script and it works without crashing,  Would this be a bug then that the kill timer does not work in 64 bit applications?

 

#AutoIt3Wrapper_UseX64=n

 

Edited by iAmNewbe

Share this post


Link to post
Share on other sites

#13 ·  Posted

How is the timer to start from 00:00:00 and goes up ?

Share this post


Link to post
Share on other sites

#14 ·  Posted

9 minutes ago, SaeidN said:

How is the timer to start from 00:00:00 and goes up ?

I assume that is how it works but I don't see that level of detail in the help documentation. 

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

I am on x64 too b/c I am on Windows 10.  I ran your code and it works fine.  It does not crash for me - it exits gracefully.  Can you show the Scite output after the script stops working?  Can you add some debugging too?  I assume the script is exactly as you have posted it above?

EDIT: can anyone else replicate the crash on x64 using Au3  3.3.14.2?

Edited by Jfish
1 person likes this

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

#16 ·  Posted

Console Window Output

 

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "D:\Projects\Project - AutoIT\Games & Learning Projects\Slot Machine\timer_breaks.au3" /UserParams    
+>19:47:49 Starting AutoIt3Wrapper v.16.612.1119.0 SciTE v.3.6.6.0   Keyboard:00000409  OS:WIN_10/  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\Main\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\Main\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.2)  from:C:\Program Files (x86)\AutoIt3  input:D:\Projects\Project - AutoIT\Games & Learning Projects\Slot Machine\timer_breaks.au3
+>19:47:49 AU3Check ended.rc:0
>Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\autoit3_x64.exe "D:\Projects\Project - AutoIT\Games & Learning Projects\Slot Machine\timer_breaks.au3"    
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
case 1
case 3
!>19:47:55 AutoIt3.exe ended.rc:-1073741819
+>19:47:55 AutoIt3Wrapper Finished.
>Exit code: 3221225477    Time: 6.574



I reinstalled AutoIT 3.3.14.2 and set it to default to x86 instead of x64 so I added the use x64 at top of file. Reposting current code below.

 

#AutoIt3Wrapper_UseX64=y
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Timers.au3>

Global $Form1 = GUICreate("Form1", 615, 437, 192, 124)
GUISetState(@SW_SHOW)

Global $myTimer = _Timer_SetTimer($Form1, 1000, "_TimedActivated") ; create timer


Func _TimedActivated($hWnd, $iMsg, $iIDTimer, $iTime)
Dim $Form1, $myTimer
  Local $choice = Random(1,3,1)
    ;ConsoleWrite($choice&@crlf)
    Switch $choice
        Case 1
            ConsoleWrite("case 1"&@crlf)
        Case 2
            ConsoleWrite("case 2"&@crlf)
        Case 3
            _Timer_KillAllTimers ($Form1)
            ConsoleWrite("case 3"&@crlf)
            Exit
    EndSwitch
 EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _Timer_KillAllTimers ($Form1)
            Exit
    EndSwitch
WEnd

 

Share this post


Link to post
Share on other sites

#17 ·  Posted

What do you mean by adding debugging?  I just use msgbox or console write to see if I get the output needed but if the app crashes I look into the console window to see if there is any info there about the crash.  

Share this post


Link to post
Share on other sites

#19 ·  Posted

Changed the line and the output was "True"

Console Window Output
 

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "D:\Projects\Project - AutoIT\Games & Learning Projects\Slot Machine\timer_breaks.au3" /UserParams    
+>20:04:18 Starting AutoIt3Wrapper v.16.612.1119.0 SciTE v.3.6.6.0   Keyboard:00000409  OS:WIN_10/  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\Main\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\Main\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.2)  from:C:\Program Files (x86)\AutoIt3  input:D:\Projects\Project - AutoIT\Games & Learning Projects\Slot Machine\timer_breaks.au3
+>20:04:18 AU3Check ended.rc:0
>Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\autoit3_x64.exe "D:\Projects\Project - AutoIT\Games & Learning Projects\Slot Machine\timer_breaks.au3"    
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
True
!>20:04:25 AutoIt3.exe ended.rc:-1073741819
+>20:04:25 AutoIt3Wrapper Finished.
>Exit code: 3221225477    Time: 7.064


Copied the file to my desktop and ran it with same result and crash.

Console Output 

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\Main\Desktop\timer_breaks.au3" /UserParams    
+>20:06:31 Starting AutoIt3Wrapper v.16.612.1119.0 SciTE v.3.6.6.0   Keyboard:00000409  OS:WIN_10/  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\Main\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\Main\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.2)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\Main\Desktop\timer_breaks.au3
+>20:06:31 AU3Check ended.rc:0
>Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\autoit3_x64.exe "C:\Users\Main\Desktop\timer_breaks.au3"    
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
case 1
case 1
case 2
True
!>20:06:39 AutoIt3.exe ended.rc:-1073741819
+>20:06:39 AutoIt3Wrapper Finished.
>Exit code: 3221225477    Time: 8.791



I looked in the manual and found out about debugging and when I ran with debugging the output in the debug window after app crashed was "Operation completed successfully" in regard to the _KillAllTimers function.  So it works then the app crashes.  Removing or setting the script to run in x86 mode and not x64 it works normally no crashing.

Share this post


Link to post
Share on other sites

#20 ·  Posted

You are using a different version of AutoIT than I am so maybe something changed in the newest version?

Share this post


Link to post
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