回调函数调用
void CALLBACK ClientFileCallBack(HANDLE handle,DWORD dwDataType,BYTE* pBuffer,DWORD dwBufSize,void* pUser )
{
CFileAppDlg*dlg=(CFileAppDlg*)pUser;
dlg->OnFileCallback(handle,dwDataType,pBuffer,dwBufSize);
}
有函数OpenPlayer(szHost,szFile,ClientFileCallBack,this)其中ClientFileCallBack是回调函数,this是用户回调信息,我想知道ClientFileCallBack中的函数什么时候被调用,是OpenPlayer(szHost,szFile,ClientFileCallBack,this)这行代码被执行时么?回调函数中的参数是怎么被传递进入的,这个this怎么在回调函数中成了pUser 回调函数? 参数传递
[解决办法]
OpenPlayer(szHost,szFile,ClientFileCallBack,this)函数被调用后
在函数OpenPlayer 执行的某段代码中,会调用回调函数
就像计算积分的函数
sin(x) = ∫cos(t) dt (t=0 ~ x )
假设函数 cos(x) 已经定义了
double cos(double x);
typedef double (*CallBackForIntegral)(double);
double Integral(CallBackForIntegral callbackFunc,double a,double b)
{
double fa= callbackFunc(a);
long n=1000;
double fx=0,s=0;
for( i=0;i<n;i++)
{
fx= callbackFunc((a+b)*i/n;
s+= (fa+ fx)/2 * (a+b)/n;
}
return s;
}
double sin(double x)
{
return Integral(cos,0,x);
}
PS:
这个求利用余弦和积分求正弦的方法---sin函数的实现里
Integral 这个求积分的函数,调用了回调函数cos
在函数Integral内部 ,不断调用回调函数,最终求出结果函数cos的积分sin(x)。
实际上,回调函数并不神秘,就是函数指针而已,至少是使用函数指针或者类似的方法实现的。
在使用回调函数的函数里,回调函数会被调用0~ N次。
[解决办法]
OpenPlayer里的伪代码可能如下
OpenPlayer()
{
m_funcall = ClientFileCallBack;//把回调函数保存
m_pDlg = this;//把传入的this指针保存
}
然后调用m_funcall (...m_pDlg);至于在哪里调用,回调函数本来就不是给你调用的,是你写好给别人去调用的,自然是别人在需要的地方去调用
[解决办法]
Windows系统:“不要调用我,请先填写好未来收到某个消息时你的处理流程,在那个消息到来时我会调用你!”
#pragma comment(lib,"user32")
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include <windows.h>
char datestr[16];
char timestr[16];
char mss[4];
void log(char *s) {
struct tm *now;
struct timeb tb;
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,s);
}
VOID CALLBACK myTimerProc1(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
) {
log("In myTimerProc1\n");
}
VOID CALLBACK myTimerProc2(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
) {
log("In myTimerProc2\n");
}
int main() {
int i;
MSG msg;
SetTimer(NULL,0,1000,myTimerProc1);
SetTimer(NULL,0,2000,myTimerProc2);
for (i=0;i<20;i++) {
Sleep(500);
log("In main\n");
if (GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
//2012-07-26 17:29:06.375 In main
//2012-07-26 17:29:06.875 In myTimerProc1
//2012-07-26 17:29:07.375 In main
//2012-07-26 17:29:07.875 In myTimerProc2
//2012-07-26 17:29:08.375 In main
//2012-07-26 17:29:08.375 In myTimerProc1
//2012-07-26 17:29:08.875 In main
//2012-07-26 17:29:08.875 In myTimerProc1
//2012-07-26 17:29:09.375 In main
//2012-07-26 17:29:09.890 In myTimerProc2
//2012-07-26 17:29:10.390 In main
//2012-07-26 17:29:10.390 In myTimerProc1
//2012-07-26 17:29:10.890 In main
//2012-07-26 17:29:10.890 In myTimerProc1
//2012-07-26 17:29:11.390 In main
//2012-07-26 17:29:11.890 In myTimerProc2
//2012-07-26 17:29:12.390 In main
//2012-07-26 17:29:12.390 In myTimerProc1
//2012-07-26 17:29:12.890 In main
//2012-07-26 17:29:12.890 In myTimerProc1
//2012-07-26 17:29:13.390 In main
//2012-07-26 17:29:13.890 In myTimerProc2
//2012-07-26 17:29:14.390 In main
//2012-07-26 17:29:14.390 In myTimerProc1
//2012-07-26 17:29:14.890 In main
//2012-07-26 17:29:14.890 In myTimerProc1
//2012-07-26 17:29:15.390 In main
//2012-07-26 17:29:15.890 In myTimerProc2
//2012-07-26 17:29:16.390 In main
//2012-07-26 17:29:16.390 In myTimerProc1
//2012-07-26 17:29:16.890 In main
//2012-07-26 17:29:16.890 In myTimerProc1
//2012-07-26 17:29:17.390 In main
//2012-07-26 17:29:17.890 In myTimerProc2
//2012-07-26 17:29:18.390 In main
//2012-07-26 17:29:18.390 In myTimerProc1
//2012-07-26 17:29:18.890 In main
//2012-07-26 17:29:18.890 In myTimerProc1
//2012-07-26 17:29:19.390 In main
//2012-07-26 17:29:19.890 In myTimerProc2