回调函数【Delphi版】 |
您所在的位置:网站首页 › 函数原型中可以不指明参数的类型是 › 回调函数【Delphi版】 |
一、纯回调函数 定义回调函数的原型:指明函数参数类型和返回值类型 1 type2 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 else10 Result:=False11 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 implementation13 uses14 Main;15 constructor TDemoThread.Create(RequestStr:string;globalCallBackFunc:TCallBackFuncType;CreateSuspended: Boolean);16 begin17 inherited Create(CreateSuspended);//CreateSuspended指定创建之后是否挂起18 Self.RequestStr:=RequestStr;19 Self.globalCallBackFunc:=globalCallBackFunc;20 end;21 22 procedure TDemoThread.Execute;23 begin24 inherited;25 26 if globalCallBackFunc(RequestStr) then //线程内部通过获得的函数指针调用线程外部的“回调函数”27 MessageBox(Form1.Handle,PChar('回调函数测试成功,测试字符串中含有子串:China'),PChar('回调函数调用结果'),MB_OK)28 else29 MessageBox(Form1.Handle,PChar('回调函数测试成功,测试字符串中无子串:China'),PChar('回调函数调用结果'),MB_OK);30 31 FreeOnTerminate:=True;//线程一旦停止,就释放内存32 end;33 34 destructor TDemoThread.Destroy;35 begin36 inherited;37 end;创建线程,并启动 1 begin2 //将回调函数的函数指针传给线程3 myDemoThread:=TDemoThread.Create('China',ThreadCallBackFunc,True);4 myDemoThread.Resume;5 end;运行结果如下: 二、事件模型中的回调函数 定义回调函数的原型:指明函数参数类型和返回值类型 1 type2 TOnThreadExecuteFuncType = function(RequestStr:string):Boolean of object; //定义事件模型中的回调函数原型定义某个对象的成员函数,作为回调函数 注:一定要是某个对象的成员函数,而不能是全局函数,这是事件模型与纯回调最大的区别 1 function TForm1.OnThreadExecuteCallBackFunc(RequestStr:string):Boolean; //被传入的函数一定要是“某个对象的成员函数”2 begin //不能使用全局函数 3 if Pos('China',RequestStr)>0 then4 Result:=True5 else6 Result:=False7 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 implementation13 uses14 Main;15 constructor TDemoThread.Create(RequestStr:string;OnThreadExecuteFunc:TOnThreadExecuteFuncType;CreateSuspended: Boolean);16 begin17 inherited Create(CreateSuspended);//CreateSuspended指定创建之后是否挂起18 Self.RequestStr:=RequestStr;19 Self.OnThreadExecuteFunc:=OnThreadExecuteFunc;20 end;21 22 procedure TDemoThread.Execute;23 begin24 inherited;25 26 if OnThreadExecuteFunc(RequestStr) then //线程内部通过获得的函数指针调用线程外部的“回调函数”27 MessageBox(Form1.Handle,PChar('回调函数测试成功,测试字符串中含有子串:China'),PChar('回调函数调用结果'),MB_OK)28 else29 MessageBox(Form1.Handle,PChar('回调函数测试成功,测试字符串中无子串:China'),PChar('回调函数调用结果'),MB_OK);30 31 FreeOnTerminate:=True;//线程一旦停止,就释放内存32 end;33 34 destructor TDemoThread.Destroy;35 begin36 inherited;37 end;创建线程,并启动 1 begin2 myDemoThread:=TDemoThread.Create('China',OnThreadExecuteCallBackFunc,True);//此处传入的方法一定要是“某个对象的成员函数”3 myDemoThread.Resume; //不能使用全局函数4 end;运行结果如下: 说明:1.回调含义的由来(即:为什么叫回调函数) 在使用回调函数时,整个调用过程如下图所示: 整个处理过程是:a)调用实体(同时将回调函数的函数指针传递给实体) b)实体开始执行 c)执行到某一步时,实体又调用到在实体之外定义的函数(根据回调函数的函数指针来访问) 注:“实体”可以是一个线程、一个函数,也可以是一个对象 2.关于“纯回调函数”与“事件模型中的回调函数”的比较 在机制上,“纯回调函数”和“事件模型的回调函数”是一样的。但具体形式有些区别: 纯回调函数:全局函数的形式 事件模型中的回调函数:对象方法的形式
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |