//计时器函数
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请问改如何实现静态函数中调用非静态函数?

解决方案 »

  1.   

    直接用AfxBeginThread(RunThread, this); 
      

  2.   

    void  CALLBACK CWeatherDlg::TimeProc
    这个函数是静态的,用不了this啊
    现在我是直接经TimeProc的第一个参数传给AfxBeginThread(
    AfxBeginThread(RunThread, (LPVOID)hwnd);
    然后再线程函数中又转换过来:
    //线程函数
    UINT CWeatherDlg::RunThread(LPVOID pParam )
    {
    CWeatherDlg *ptr=(CWeatherDlg*)(HWND *)pParam;
    CString cityName="厦门";
    ptr->WeahUpdate(cityName);
    return 1;
    }
    这样是能够运行,但是不知道会不会存在什么潜在的问题?
      

  3.   


    直接用AfxBeginThread(RunThread, this); 绝对可以还有, 如果你用静态指针的话, 在静态函数里就能直接用, 不用传参转换.
      

  4.   

    静态函数只是没有本身的this而已,你传一个指针进去当然能用的..........
      

  5.   

    this 指针在静态函数中不存在,怎么传啊
      

  6.   

    this 指针在静态函数中不存在,怎么传啊 
      

  7.   

    void  CALLBACK CWeatherDlg::TimeProc(HWND hwnd,UINT message,UINT iTimerID,DWORD dwTime) 
    这个回调函数怎么传this指针,回调函数参数都是固定的吧,该如何实现呢?
      

  8.   

    //计时器函数
    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指针
      

  9.   

    1楼的已经给你贴出代码了,呵呵
    this就这样传就行~
      

  10.   

    最好的方法是:
    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); 
      

  11.   

    //在主对话框里添加该消息的处理函数
    OnUserWeahUpdate(WPARAM wp,LPARAM lp)