question

AKR-6718 avatar image
0 Votes"
AKR-6718 asked ·

Kill Timer doesnot end the timer - WINAPI c++

Hallo,

I have been working a application which communicates with the micro controller with com interface and send status request and receive the answer every few seconds. I used set timer for the same. When there is a change in status warning message should pop up only once even if the status change still persists in the next packet received. But the warning message is popping up every time the packet is received . The timer flags does not work. Please let me know if i am missing something


void CALLBACK StatusRequest(HWND StatusHwnd, UINT uMsg, UINT timerId, DWORD dwTime){


ret = sendReceivePacket(........);

TIMEFLAG = false ;


if (TIMEFLAG == false)
{
if(ret ==0) // change is status
{
//warning message//
TIMEFLAG = True;
.................
}
else
{
}

}

INT_PTR CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wP, LPARAM lP){

switch (msg){
case WM_INITDIALOG:

SetTimer(NULL, TIMER_ID_SEND_REQUEST, 1000 * 3, (TIMERPROC)StatusRequest);

case WM_TIMER:

KillTimer(NULL, TIMER_ID_SEND_REQUEST);

}
}




The DLg proc is the main dialog function with different messages. Timeflag was used to stop the warning message to print everytime but it is not working. Kill timer didnot work as well.


c++winapi-generalwinapi-sdk
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SongZhu-MSFT avatar image
1 Vote"
SongZhu-MSFT answered ·

According to MSDN:

If the function succeeds and the hWnd parameter is NULL, the return value is an integer identifying the new timer. An application can pass this value to the KillTimer function to destroy the timer.


So you need to modify the code to:

 UINT_PTR id = 0;
    
 ......
    
 id = SetTimer(NULL, TIMER_ID_SEND_REQUEST, 1000 * 3, (TIMERPROC)StatusRequest);
    
 ......
    
 KillTimer(NULL, id);






·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

GuidoFranzke avatar image
0 Votes"
GuidoFranzke answered ·

Hello,
how do you declare the variable TIMEFLAG? It looks like it's global, so this is ok. But you should not set the flag to false in the routine. Set it to false again, when you need it.

 bool TIMEFLAG = false;
    
 void CALLBACK StatusRequest(HWND StatusHwnd, UINT uMsg, UINT timerId, DWORD dwTime){
    
 ret = sendReceivePacket(........);
    
 //TIMEFLAG = false ;

Regards, Guido

· 3 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello,
Thank you for mentioning. In the actual code it is declared globally. I just wanted to paste important lines and ended up pasting it wrong.

0 Votes 0 ·

Still the same: don't set "TIMEFLAG=false;" in the beginning of the routine.
When you know that you received all pakets for one message, then you must set TIMEFLAG to false again.

0 Votes 0 ·

the Timeflag is not set there is actual code. That was mistake i did when i was pasting each line. It is set after the message is received.

 ret = sendReceivePacket(........);
    
 if (TIMEFLAG == false)
 {
 if(ret ==0) // change is status
 {
 //warning message//
 TIMEFLAG = True;
 .................
 }
 else
 {
 }
    
 }
    
 INT_PTR CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wP, LPARAM lP){
    
 switch (msg){
 case WM_INITDIALOG:
    
 SetTimer(NULL, TIMER_ID_SEND_REQUEST, 1000 * 3, (TIMERPROC)StatusRequest);
    
 case WM_TIMER:
    
 KillTimer(NULL, TIMER_ID_SEND_REQUEST);
    
 }
 }


0 Votes 0 ·
KingKong-4442 avatar image
1 Vote"
KingKong-4442 answered ·

Regarding KillTimer, parameter 2, the documentation says: " If the application calls SetTimer with hWnd set to NULL, this parameter must be the timer identifier returned by SetTimer."

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-killtimer

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AKR-6718 avatar image
0 Votes"
AKR-6718 answered ·

The issue with my code actually came from flag set somewhere else in the code. It is now solved. But thank you all of your suggestions on Kill Timer. I will make the changes in my code.

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You can post an answer and accept or accept an answer that you think is reasonable. This will be helpful to the community, thank you.

1 Vote 1 ·