一、纯回调函数

定义回调函数的原型:指明函数参数类型和返回值类型

1 type
2   TCallBackFuncType = function(RequestStr:string):boolean; stdcall; //定义回调函数原型

定义某个具体的全局函数,并指明是回调函数

1 function ThreadCallBackFunc(RequestStr:string):boolean;stdcall;
 2 
 3 implementation
 4 
 5 function ThreadCallBackFunc(RequestStr:string):boolean;stdcall;
 6 begin
 7   if Pos('China',RequestStr)>0 then
 8     Result:=True
 9   else
10     Result:=False
11 end;

定义一个线程,该线程会调用上面的回调函数

1 type
 2   TDemoThread=class(TThread)
 3     private
 4       RequestStr:string;
 5       globalCallBackFunc:TCallBackFuncType;
 6     public
 7       constructor Create(RequestStr:string;globalCallBackFunc:TCallBackFuncType;CreateSuspended: Boolean);
 8       procedure  Execute;override;
 9       destructor Destroy;override;
10   end;
11 
12 implementation
13 uses
14   Main;
15 constructor TDemoThread.Create(RequestStr:string;globalCallBackFunc:TCallBackFuncType;CreateSuspended: Boolean);
16 begin
17   inherited Create(CreateSuspended);//CreateSuspended指定创建之后是否挂起
18   Self.RequestStr:=RequestStr;
19   Self.globalCallBackFunc:=globalCallBackFunc;
20 end;
21 
22 procedure  TDemoThread.Execute;
23 begin
24   inherited;
25 
26   if globalCallBackFunc(RequestStr) then  //线程内部通过获得的函数指针调用线程外部的“回调函数”
27     MessageBox(Form1.Handle,PChar('回调函数测试成功,测试字符串中含有子串:China'),PChar('回调函数调用结果'),MB_OK)
28   else
29     MessageBox(Form1.Handle,PChar('回调函数测试成功,测试字符串中无子串:China'),PChar('回调函数调用结果'),MB_OK);
30 
31   FreeOnTerminate:=True;//线程一旦停止,就释放内存
32 end;
33 
34 destructor TDemoThread.Destroy;
35 begin
36   inherited;
37 end;

创建线程,并启动

1 begin
2    //将回调函数的函数指针传给线程
3      myDemoThread:=TDemoThread.Create('China',ThreadCallBackFunc,True);
4    myDemoThread.Resume;
5 end;

运行结果如下:

    

android delphi 返回按钮 delphi回调函数_Boo

二、事件模型中的回调函数

定义回调函数的原型:指明函数参数类型和返回值类型

1 type
2   TOnThreadExecuteFuncType = function(RequestStr:string):Boolean of object; //定义事件模型中的回调函数原型

定义某个对象的成员函数,作为回调函数

一定要是某个对象的成员函数,而不能是全局函数,这是事件模型与纯回调最大的区别

1 function TForm1.OnThreadExecuteCallBackFunc(RequestStr:string):Boolean; //被传入的函数一定要是“某个对象的成员函数”
2 begin                                                                   //不能使用全局函数 
3   if Pos('China',RequestStr)>0 then
4     Result:=True
5   else
6     Result:=False
7 end;

定义一个线程,该线程会调用上面的回调函数

1 type
 2   TDemoThread=class(TThread)
 3     private
 4       RequestStr:string;
 5       OnThreadExecuteFunc:TOnThreadExecuteFuncType;
 6     public
 7       constructor Create(RequestStr:string;OnThreadExecuteFunc:TOnThreadExecuteFuncType;CreateSuspended: Boolean);
 8       procedure  Execute;override;
 9       destructor Destroy;override;
10   end;
11 
12 implementation
13 uses
14   Main;
15 constructor TDemoThread.Create(RequestStr:string;OnThreadExecuteFunc:TOnThreadExecuteFuncType;CreateSuspended: Boolean);
16 begin
17   inherited Create(CreateSuspended);//CreateSuspended指定创建之后是否挂起
18   Self.RequestStr:=RequestStr;
19   Self.OnThreadExecuteFunc:=OnThreadExecuteFunc;
20 end;
21 
22 procedure  TDemoThread.Execute;
23 begin
24   inherited;
25 
26   if OnThreadExecuteFunc(RequestStr) then  //线程内部通过获得的函数指针调用线程外部的“回调函数”
27     MessageBox(Form1.Handle,PChar('回调函数测试成功,测试字符串中含有子串:China'),PChar('回调函数调用结果'),MB_OK)
28   else
29     MessageBox(Form1.Handle,PChar('回调函数测试成功,测试字符串中无子串:China'),PChar('回调函数调用结果'),MB_OK);
30 
31   FreeOnTerminate:=True;//线程一旦停止,就释放内存
32 end;
33 
34 destructor TDemoThread.Destroy;
35 begin
36   inherited;
37 end;

创建线程,并启动

1 begin
2   myDemoThread:=TDemoThread.Create('China',OnThreadExecuteCallBackFunc,True);//此处传入的方法一定要是“某个对象的成员函数”
3   myDemoThread.Resume;                                                       //不能使用全局函数
4 end;

运行结果如下:

    

android delphi 返回按钮 delphi回调函数_Boo_02

说明:1.回调含义的由来(即:为什么叫回调函数)

在使用回调函数时,整个调用过程如下图所示:

android delphi 返回按钮 delphi回调函数_Boo_03

           整个处理过程是:a)调用实体(同时将回调函数的函数指针传递给实体)

                                   b)实体开始执行

                                   c)执行到某一步时,实体又调用到在实体之外定义的函数(根据回调函数的函数指针来访问)

注:“实体”可以是一个线程、一个函数,也可以是一个对象

        2.关于“纯回调函数”与“事件模型中的回调函数”的比较

           在机制上,“纯回调函数”和“事件模型的回调函数”是一样的。但具体形式有些区别:

           纯回调函数:全局函数的形式

           事件模型中的回调函数:对象方法的形式