• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

回调函数【Delphi版】

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、纯回调函数

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

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;

     运行结果如下:

    

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

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

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;

     运行结果如下:

    

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

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

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

                                   b)实体开始执行

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

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

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

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

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

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

           


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab_exercise(4)----第一题发布时间:2022-07-18
下一篇:
matlab作业之m文件的建立与使用运行发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap