Thursday, March 08, 2007

call the nonstatic member function in a Timer's static callback fn....

How can we call the nonstatic member function in a Timer callback fn....
-------------------------------------------------------------------------

we can set only the static member function as Timer procedure... if we want to access the non static member function that is not possible becuase within a
static timer callback function, we can call only static members.
How can we overcome this problem ?
Here we used the Global variable to hold the value of the Object...


Sample application ...

CTest *pGlobalTestObject;
class CTest
{

public:
CTest()
{
}
bool StartTimer();
bool KillTimer(UINT uTimerId);

static void CALLBACK TimerProc_Wrapper(HWND hwnd, UINT uMsg, UINT uIdEvent,
DWORD dwTime);

void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT uIdEvent,
DWORD dwTime);

//Inorder to call the non static member function we have to use Global Object...
};

void CALLBACK CTest::TimerProc_Wrapper(HWND hwnd, UINT uMsg, UINT uIdEvent,
DWORD dwTime)
{
CTest * pTest = pGlobalTestObject;
pTest->TimerProc(hwnd,uMsg,uIdEvent,dwTime);
}
void CALLBACK CTest::TimerProc(HWND hwnd, UINT uMSg,UINT uIdEvent, DWORD dwTime)
{
// Do operations on class member functions
}
bool CTest::StartTimer()
{
pGlobalTestObject = this;
SetTimer(hwnd,Timer_ID,Timer_MilliSeconds,TimerProc_Wrapper );
return true;
}
bool CTest::KillTimer(UINT uTimerId)
{
return KillTimer(hwnd, uTimerId);
}

we may extent this problem as How can we make use of calling nonstatic member function in static member fn..

No comments: