Jump to content

How do you use a Timer?


 Share

Recommended Posts

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

 

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

 

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


 

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

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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

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