Jump to content

RegisterWaitForSingleObject for handling events with callback


GtaSpider
 Share

Recommended Posts

Hey everyone,

I searched some hours for a way to use the function WaitForSingleObject not as a function the script has to wait for, but a function which calls a callback function when the event is signaled. I found a way with RegisterWaitForSingleObject  but no AutoIt implemention, so I made one on my own, and now I want to share it with you guys!

The only limitation by this implemention is that you are not able to use more than one callback at once. to avoid this, you could write a static array in the function which fills with the selfmade callback structs. If you find another way, let me know!

Example:

#include <WinAPI.au3>
#include 'RegisterWaitForSingleObject.au3'

Global $fExit = False


Local $hPid = Run("notepad.exe")
Local $hProcess = _WinAPI_OpenProcess(0x00100000, False, $hPid) ; SYNCHRONIZE (0x00100000)
ControlSetText(WinWait("[CLASS:Notepad]"), "", "[CLASS:Edit; INSTANCE:1]", "Close Editor or wait 3 seconds for timeout")


;Registers the _TestFunc with a timeout of 3 secs.
;The _TestFunc shall be like this: _TestFunc($fTimeout)
;where $fTimeout signalise wether timeout reached (True) or event is in signaled state (False)
_WinAPI_RegisterWaitForSingleObject($hProcess, "_TestFunc", 3000)


While Not $fExit
    ConsoleWrite("Do some stuff at " & @HOUR & ":" & @MIN & ":" & @SEC & "." & @MSEC & @CRLF)
    Sleep(500)
WEnd

ConsoleWrite("Bye" & @CRLF)
_WinAPI_CloseHandle($hProcess)


Func _TestFunc($fTimeout)
    If $fTimeout Then
        ConsoleWrite("Timeout reached" & @CRLF)

    Else
        ConsoleWrite("Object is in signaled state" & @CRLF)
    EndIf
    $fExit = True
EndFunc   ;==>_TestFunc

Hope you like it and find use for it!

Greetz,

Spider

RegisterWaitForSingleObject.zip

www.AutoIt.de - Moderator of the German AutoIt Forum

 

Link to comment
Share on other sites

  • Moderators

Am I missing it, or do you never free the DllCallbackRegister?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Am I missing it, or do you never free the DllCallbackRegister?

 

Yup, you're rigth. But I initalize it as a static variable; therefore it's only initalized one time, the same Callback for all functions (the Handler function never changes so there is no need to initalize DllCallbackRegister every time)

Greetings,

Spider

www.AutoIt.de - Moderator of the German AutoIt Forum

 

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

×
×
  • Create New...