Kill Timer doesnot end the timer - WINAPI c++

AKR 21 Reputation points
2021-04-08T11:36:32.547+00:00

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.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,412 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,520 questions
0 comments No comments
{count} votes

Accepted answer
  1. Song Zhu - MSFT 906 Reputation points
    2021-04-09T05:52:31.14+00:00

    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);  
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. KingKong-4442 166 Reputation points
    2021-04-08T20:30:00.337+00:00

    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://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-killtimer

    1 person found this answer helpful.
    0 comments No comments

  2. Guido Franzke 2,196 Reputation points
    2021-04-08T12:03:16.997+00:00

    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. AKR 21 Reputation points
    2021-04-09T07:12:07.74+00:00

    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.