Hi,I'm looking for some help using SetTimer & thread.
What I want to do is: keep checking the memory every 3 seconds. If the memory is low, I popup a message and ask the user to Exit from the application or close other applications and continue with the present one. There are Stop & Continue buttons. From the time I popup this dialog box, I have to wait only for 20 seconds. If there's no response from the user, I simply assume that he wants to quit the application and exit. However, before I quit, I want to recheck the memory status now. If the memory is still low, I have to Exit. Otherwise, close the dialog and continue with the application. But the memory check should continue as long as the application is running.
I created a thread. starting a timer within the thread callback. The timer procedure will actually do all the memcheck. if
there's less memory, i display a message with Stop & Continue buttons. Call another timer for about 15 seconds.
If there's no user input (Stop or Continue), the second timer proc is called, it should again check the memory. if the memory is still less then post a WM_QUIT msg, otherwise, kill the second timer and continue with the application.
If there's a user input, "Stop" I call WM_QUIT msg and for "Continue", kill both the timers and call the memory check function again.
The reason for creating the thread is I dont want the timer to be running in the main thread.
The reason for starting the 2nd timer is, I want to allow some time for the memory refresh and meanwhile the user closes other applications and release some memory.
But by the end of the second timer (15 seconds), the memory is still not released, I post a forcible quit of the main
application and exit.
However, I have only one timer proc for both the timers.
Now the prob:
The condition for the second timer in the timer proceduce is never satisfied. As a result, the second timer runs in an infinite loop using 100% CPU time and high memory and never gets killed.
Given below the sample code. Could anyone figure out what could be the prob. Also any other better way of doing it.
Thanks in advance.
#define AUTO_CLOSE_MEM_ERR_DLG 100
MemCheck( )
{
//Here goes Createthread code.
}
// Tread callback
ThreadProc( )
{
iTimer1 = ::SetTimer(NULL,1, 15000, (TIMERPROC) timerProc);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// timer callback
timerProc( UINT nIDEvent)
{
//Here goes the mem check code
if (nIDEvent == AUTO_CLOSE_MEM_ERR_DLG)
{
// IT NEVER COMES TO THIS CODE
KillTimer(NULL, AUTO_CLOSE_MEM_ERR_DLG);
if (AvlMem < 1024)
{
g_bContinue = FALSE;
StopTimer();
PostMessage(pFrame->m_hWnd, WM_QUIT,0,0);
}
else
g_bContinue = TRUE;
}
else
{
if (AvlMem < 1024)
{
//Low Memory dialog should come here with Stop&Continue buttons.
SetTimer(NULL, AUTO_CLOSE_MEM_ERR_DLG, 15000, (TIMERPROC) timerProc);
}
}
}
OnStop()
{
StopTimer();
// Exit from appln
}
OnContinue()
{
StopTimer();
MemCheck();
}