@lexikos I remember that in the old forum I wrote a library using RegisterWaitForSingleObject with an ordinary AHK callback and you outlined the mistakes in that approach, mentioning machine code callbacks as an alternative.
Now after all this time I need this code again and it's cool to see it in V2.
I slightly modified OpenProcess to allow for arbitrary data to be passed to the ProcessHandle wrapper:
Now after all this time I need this code again and it's cool to see it in V2.
I slightly modified OpenProcess to allow for arbitrary data to be passed to the ProcessHandle wrapper:
CODE:
/*
OnProcessClose:
Registers *callback* to be called when the process identified by *proc*
exits, or after *timeout* milliseconds if it has not exited.
@arg proc - A process handle as either an Integer or an object with `.ptr`.
@arg callback - A function with signature `callback(handle, timedOut)`.\
`handle` is a ProcessHandle with properties `ID`, `ExitCode` and `Ptr` (process handle).\
`timedOut` is true if the wait timed out, otherwise false.
@arg data - Arbitrary data to be passed to the ProcessHandle object.
@arg timeout - The timeout in milliseconds. If omitted or -1, the wait
never times out. If 0, the callback is called immediately.
@returns {RegisteredWait} - Optionally use the `Unregister()` method
of the returned object to unregister the callback.
*/
OnProcessClose(proc, callback, data?, timeout?) {
if !(proc is Integer || proc := ProcessExist(proc))
throw ValueError("Invalid PID or process name", -1, proc)
if !proc := DllCall("OpenProcess", "uint", 0x101000, "int", false, "uint", proc, "ptr")
throw OSError()
return RegisterWaitCallback(ProcessHandle(proc, data?), callback, timeout?)
}
class ProcessHandle {
__new(handle, data:="") {
this.ptr := handle
this.data := data
}
__delete() => DllCall("CloseHandle", "ptr", this)
ID => DllCall("GetProcessId", "ptr", this)
ExitCode {
get {
if !DllCall("GetExitCodeProcess", "ptr", this, "uint*", &exitCode:=0)
throw OSError()
return exitCode
}
}
}
#include RegisterWaitCallback.ahk
Statistics: Posted by cyruz — Today, 16:06