12

I would like to know if it's possible to implement something like this:

procedure waitandcall(time,@MyProcedureOrFunction)

which execute the function or procedure I've created ?

I'm not sure if they are called callbacks.

Johan
  • 74,508
  • 24
  • 191
  • 319
ELCouz
  • 572
  • 2
  • 6
  • 17

2 Answers2

26

It's absolutely possible, and you don't even need @ before your function.

In fact, all the events are based on this.

Start by defining a type for your function/procedure

type
  TMyProc = procedure(Param1: Integer);

Then you can use your procedure type anywhere, as long as your procedure's signature matches your type.

If you're using an object method instead of a plain procedure/function, you need to use of object

type
  TMyMethod = procedure(Param1: Integer) of object;

To call your callback from within, you can use something like this:

procedure DoAndCallBack(MyProc: TMyProc)
begin
  MyProc(1);
end;
ESG
  • 8,988
  • 3
  • 35
  • 52
5

Callbacks are exactly what those are called, and Delphi is entirely capable of both creating and calling them. They are also known as function pointers. See Procedural Types in the documentation.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • Do you have an example how to use them properly? I mean what I need to write inside the waitandcall function to execute the procedure? – ELCouz Dec 28 '12 at 05:18
  • 2
    If it does what its name suggests, you can do it in two instructions: `Sleep(t); F;` – Rob Kennedy Dec 28 '12 at 05:21
  • Basically i wanted a "blocking" procedure (for a thread) which count time (gettickcount) and wait (sleep) then launch whatever procedure – ELCouz Dec 28 '12 at 05:24
  • Oh well you just made me realized that I've overthinked a little bit... just sleep(x amount of time) – ELCouz Dec 28 '12 at 05:28