How can I define callback function of timeSetEvent as an instance method?
The simple answer is that you cannot. The signature of the callback is not something that you can vary. It is defined by the API and must be a simple unbound procedure with this signature:
typedef void ( CALLBACK *LPTIMECALLBACK)(
UINT uTimerID,
UINT uMsg,
DWORD_PTR dwUser,
DWORD_PTR dw1,
DWORD_PTR dw2
);
What you can do though is pass a pointer to timeSetEvent that contains the address of your instance. The pattern runs like this:
procedure TimeProcCallback(uTimerID, uMessage: UINT; dwUser,dw1,dw2: DWORD_PTR); stdcall;
begin
TUDPBC(dwUser).TimeProc(uTimerID, uMessage);
end;
This is the callback that you pass to timeSetEvent. Your class would look like this:
type
TUDPBC = class
private
FTimerID: MMRESULT;
procedure TimeProc(uTimerID, uMessage: UINT);
end;
This method of the class will be called by the callback function and so have access to the instance variables.
Set the timer like this:
FTimerID := timeSetEvent(TimeOut, uRes, TimeProcCallback, DWORD_PTR(Self), TIME_ONESHOT);
So, you pass the address of the instance in the dwUser argument. That is then passed on to your callback. That callback can, in turn, call an instance method.