4

Using AutoIt 3, is there a way to pass additional arguments to the callback method in the _Timer_SetTimer function?

Here's my use case (main loop) :

For $item In $items
    _Timer_SetTimer(0, $timeOffset, "MyMethod")
Next

Callback method :

Func MyMethod($hWnd, $iMsg, $iTimerID, $iTime)
    _Timer_KillTimer ( $hWnd, $iTimerID )

    // Do something on $item

EndFunc

I tried using a Global variable, but every single instance of MyMethod then uses the last value. I did it this way :

Global $currentItem

For $item In $items
    $currentItem = $item
    _Timer_SetTimer(0, $timeOffset, "MyMethod")
Next

Func MyMethod($hWnd, $iMsg, $iTimerID, $iTime)
    _Timer_KillTimer ( $hWnd, $iTimerID )

    $item = $currentItem
    // Do something on $item

EndFunc

So, am I doing it wrong or is there a way to pass argument directly? Thanks.

Thrax
  • 1,926
  • 1
  • 17
  • 32

1 Answers1

3

If your delayed calls are ordered, you could still use a Global variable to store the values in an Array :

Global $values[0]

For $item In $items
     _ArrayAdd($values, $item)
     _Timer_SetTimer(0, $timeOffset, "MyMethod")
Next

Func MyMethod($hWnd, $iMsg, $iTimerID, $iTime)
     _Timer_KillTimer ( $hWnd, $iTimerID )

     _ArrayReverse($values)
     $item = _ArrayPop($values)
     _ArrayReverse($values)

    // Do something on $item

EndFunc

The double reverse and pop are there to simulate a FIFO queue

Juin
  • 46
  • 2