Delayed non-blocking callback function
In my previous post regarding a delayed callback function under Delphi I should have pointed out that it was coded for a form-based environment. Meaning that it allows the GUI to remain responsive and other events to fire by calling application.processmessages at a regular intervals. It was indeed “blocking” in the sense that it also functions as a friendly alternative to the sleep API function.
unit w3callback; interface uses System.SysUtils, Winapi.Windows, System.Generics.Collections; procedure w3_callback(const aCallback:TProc;ms:Integer); implementation var _LUT: TDictionary<UINT,TProc>; procedure w3_callback(const aCallback:TProc;ms:Integer); procedure w3_invoke(hwnd: HWND; uMsg: UINT; idEvent: UINT_PTR;dwTime: DWORD);stdcall; var mProc: TProc; begin KillTimer(0,idEvent); if assigned(_LUT) then begin mproc:=_lut.Items[idEvent]; _lut.Remove(idEvent); if assigned(mProc) then mproc; end; end; begin if Assigned(_LUT) then _LUT.add(SetTimer(0,0,ms,@w3_invoke),aCallback); end; initialization _LUT:=TDictionary<UINT,TProc>.Create; finalization if Assigned(_LUT) then FreeAndNil(_LUT); end.