//计时器函数
void CALLBACK CWeatherDlg::TimeProc(HWND hwnd,UINT message,UINT iTimerID,DWORD dwTime)
{
switch(iTimerID)
{
case 1:
{
CTime time;
time = CTime::GetCurrentTime();
//每天0,8,16点时进行数据更新
if ((time.GetHour()==UD_TIMEA||time.GetHour()==UD_TIMEB||time.GetHour()==UD_TIMEC)
&&(time.GetMinute()==0||time.GetMinute()==1))
{
AfxBeginThread(RunThread, (LPVOID)pWeath);
}
}
break;
default: break;
}
}
//线程函数
UINT CWeatherDlg::RunThread(LPVOID pParam )
{
CWeatherDlg *ptr=(CWeatherDlg *)pParam;
CString cityName="厦门";
ptr->WeahUpdate(cityName);
return 1;
}
pWeath是CWeatherDlg的一个静态指针,CWeatherDlg构造函数中赋值pWeath=this;
这样能编译,但是连接会有问题:
Linking...
weatherDlg.obj : error LNK2001: unresolved external symbol "public: static class CWeatherDlg * CWeatherDlg::pWeath" (?pWeath@CWeatherDlg@@2PAV1@A)
Debug/weather.exe : fatal error LNK1120: 1 unresolved externals请问改如何实现静态函数中调用非静态函数?
void CALLBACK CWeatherDlg::TimeProc(HWND hwnd,UINT message,UINT iTimerID,DWORD dwTime)
{
switch(iTimerID)
{
case 1:
{
CTime time;
time = CTime::GetCurrentTime();
//每天0,8,16点时进行数据更新
if ((time.GetHour()==UD_TIMEA||time.GetHour()==UD_TIMEB||time.GetHour()==UD_TIMEC)
&&(time.GetMinute()==0||time.GetMinute()==1))
{
AfxBeginThread(RunThread, (LPVOID)pWeath);
}
}
break;
default: break;
}
}
//线程函数
UINT CWeatherDlg::RunThread(LPVOID pParam )
{
CWeatherDlg *ptr=(CWeatherDlg *)pParam;
CString cityName="厦门";
ptr->WeahUpdate(cityName);
return 1;
}
pWeath是CWeatherDlg的一个静态指针,CWeatherDlg构造函数中赋值pWeath=this;
这样能编译,但是连接会有问题:
Linking...
weatherDlg.obj : error LNK2001: unresolved external symbol "public: static class CWeatherDlg * CWeatherDlg::pWeath" (?pWeath@CWeatherDlg@@2PAV1@A)
Debug/weather.exe : fatal error LNK1120: 1 unresolved externals请问改如何实现静态函数中调用非静态函数?
这个函数是静态的,用不了this啊
现在我是直接经TimeProc的第一个参数传给AfxBeginThread(
AfxBeginThread(RunThread, (LPVOID)hwnd);
然后再线程函数中又转换过来:
//线程函数
UINT CWeatherDlg::RunThread(LPVOID pParam )
{
CWeatherDlg *ptr=(CWeatherDlg*)(HWND *)pParam;
CString cityName="厦门";
ptr->WeahUpdate(cityName);
return 1;
}
这样是能够运行,但是不知道会不会存在什么潜在的问题?
直接用AfxBeginThread(RunThread, this); 绝对可以还有, 如果你用静态指针的话, 在静态函数里就能直接用, 不用传参转换.
这个回调函数怎么传this指针,回调函数参数都是固定的吧,该如何实现呢?
void CALLBACK TimeProc(HWND hwnd,UINT message,UINT iTimerID,DWORD dwTime)
{
switch(iTimerID)
{
case 1:
{
CTime time;
time = CTime::GetCurrentTime();
//每天0,8,16点时进行数据更新
//if ((time.GetHour()==UD_TIMEA||time.GetHour()==UD_TIMEB||time.GetHour()==UD_TIMEC)
// &&(time.GetMinute()==0||time.GetMinute()==1))
{
//
CWeatherDlg* pThis = (CWeatherDlg*)CWnd::FromHandle(hwnd);
AfxBeginThread(CWeatherDlg::RunThread,pThis);
}
}
break;
default: break;
}
}
CWeatherDlg* pThis = (CWeatherDlg*)CWnd::FromHandle(hwnd);
原来这个hwnd可以转换为this指针
this就这样传就行~
AfxBeginThread(RunThread, hwnd);自定义一个消息WM_USER_WEAHUPDATE
CString g_strCityName; //声明为全局的//线程函数里给主对话框发消息让其处理
UINT CWeatherDlg::RunThread(LPVOID pParam )
{
g_strCityName = "厦门";
PostMessage( (HWND)pParam, WM_USER_WEAHUPDATE, 0, (LPARAM)(LPCTSTR)g_strCityName);
return 0;
} //在主对话框里添加该消息的处理函数
OnUserWeahUpdate(LPCTSTR cityName);
OnUserWeahUpdate(WPARAM wp,LPARAM lp)