• Advertisement
Sign in to follow this  

Help me Kill my Timer

This topic is 2912 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

...I'm thinking we make it look like an accident... Ok, but now seriously :P. I am using timers BUT I cannot for the life of me kill my timer using KillTimer(), the timer just goes on and on. I have read msdn & looked at its example (which is really bad, it doesn't even show a TimerProc() example) & I cant get my alarm to die. Below is my simple example of what I am doing, it should compile easily so I encourage you to test it & you will see that the timer never gets shut off. Can you help me turn this timer off, (preferably without going down the TIMEPROC avenue for the 4th argument).
// Timer test

#include <windows.h>
#include <cstdlib>
#include <stdio.h>
#include <queue>

using namespace std;

#define LA_TIMER 50000

// Global Variables //
static HINSTANCE gInstance;
UINT ID;

// Functions List //

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    
    switch(msg)
    {
        case WM_CREATE:
        {                  
             
        }    
        break;
        case WM_LBUTTONDOWN:
        {
             ID = SetTimer(hwnd,LA_TIMER,1000,NULL); 
             
             // Alternate method using TIMERPROC
             // This line is wrong(wont let me compile)  coz of 4th argument
             //ID = SetTimer(hwnd,LA_TIMER,1000,(TIMERPROC)TimerProc(hwnd,LA_TIMER,LA_TIMER,GetTickCount()) );
        }     
        break;
        case WM_TIMER:
        {
             
             switch(LOWORD(wParam)) {
                        
                  case LA_TIMER:
                  {    // I cant turn the fricken timer off :(
                       MessageBox(hwnd,"Timer killed","",MB_OK);
                       KillTimer(hwnd,LA_TIMER);
                       // KillTimer(hwnd,ID); doesn't work
                  } 
                  break;    
                  default:
                  break;
             }
        } 
        break;
        default: 
        break;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}



int WINAPI WinMain(HINSTANCE gInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = gInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(DKGRAY_BRUSH);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "Custom Class";
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    // if registration of main class fails
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        "Custom Class",
        "Laser Example",
        WS_CAPTION|WS_MINIMIZEBOX|WS_VISIBLE|WS_OVERLAPPED|WS_SYSMENU,
        CW_USEDEFAULT, CW_USEDEFAULT, 600, 500,
        NULL, NULL, gInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    
    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}



Share this post


Link to post
Share on other sites
Advertisement
Are you clicking on the message box, with the left button?
Put a breakpoint on the lbuttondown and see how often that is called.

Share this post


Link to post
Share on other sites
I don't know if it's related, but your WndProc has a major problem: handled messages should return 0, not DefWindowProc which is the case now. (look at MSDN)

And this:
Quote:
Are you clicking on the message box, with the left button?
Put a breakpoint on the lbuttondown and see how often that is called.

Share this post


Link to post
Share on other sites
Quote:
Original post by RabidDog
Are you clicking on the message box, with the left button?
Put a breakpoint on the lbuttondown and see how often that is called.


No that didn't work, I also have made it so that the timer is set when you right click not left click now & the same thing happens, so its not another left click being received. I think it has to do with the KillTimer() & whether its 2nd argument is actually killing my timer?


// Timer test

#include <windows.h>
#include <cstdlib>
#include <stdio.h>
#include <queue>

using namespace std;

#define LA_TIMER 50000

// Global Variables //
static HINSTANCE gInstance;
UINT ID;

// Functions List //

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

switch(msg)
{
case WM_CREATE:
{

}
break;
case WM_RBUTTONDOWN:
{
ID = SetTimer(hwnd,LA_TIMER,1000,NULL);
break;
// Alternate method using TIMERPROC
// This line is wrong(wont let me compile) coz of 4th argument
//ID = SetTimer(hwnd,LA_TIMER,1000,(TIMERPROC)TimerProc(hwnd,LA_TIMER,LA_TIMER,GetTickCount()) );
}
break;
case WM_TIMER:
{

switch(LOWORD(wParam)) {

case LA_TIMER:
{ // I cant turn the fricken timer off :(
MessageBox(hwnd,"Timer killed","",MB_OK);
KillTimer(hwnd,LA_TIMER);
// KillTimer(hwnd,ID); doesn't work
break;
}
break;
default:
break;
}
}
break;
default:
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}



int WINAPI WinMain(HINSTANCE gInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = gInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(DKGRAY_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "Custom Class";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// if registration of main class fails
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"Custom Class",
"Laser Example",
WS_CAPTION|WS_MINIMIZEBOX|WS_VISIBLE|WS_OVERLAPPED|WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 500,
NULL, NULL, gInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}



Share this post


Link to post
Share on other sites
Quote:
Original post by gretty

No that didn't work, I also have made it so that the timer is set when you right click not left click now & the same thing happens, so its not another left click being received. I think it has to do with the KillTimer() & whether its 2nd argument is actually killing my timer?

*** Source Snippet Removed ***


KillTimer returns a bool. If it returns 0 it has failed, so you could check for that and call GetLastError to see why.

Share this post


Link to post
Share on other sites
Quote:
Original post by RabidDog
Quote:
Original post by gretty

No that didn't work, I also have made it so that the timer is set when you right click not left click now & the same thing happens, so its not another left click being received. I think it has to do with the KillTimer() & whether its 2nd argument is actually killing my timer?

*** Source Snippet Removed ***


KillTimer returns a bool. If it returns 0 it has failed, so you could check for that and call GetLastError to see why.


Whoa! S) I did what you said & it worked, thanks :), although I have no idea why it worked, all I did what create an int to store the return value of KillTimer() like you said & it worked lol

Weird but good lol
Thanks

if ur interested this is all I did
changed this:
KillTimer(hwnd,LA_TIMER);

to this:
int success = KillTimer(hwnd,LA_TIMER);


// Timer test

#include <windows.h>
#include <cstdlib>
#include <stdio.h>
#include <queue>

using namespace std;

#define LA_TIMER 50000

// Global Variables //
static HINSTANCE gInstance;
UINT ID;

// Functions List //

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

switch(msg)
{
case WM_CREATE:
{

}
break;
case WM_LBUTTONDOWN:
{
ID = SetTimer(hwnd,LA_TIMER,1000,NULL);
//break;
// Alternate method using TIMERPROC
// This line is wrong(wont let me compile) coz of 4th argument
//ID = SetTimer(hwnd,LA_TIMER,1000,(TIMERPROC)TimerProc(hwnd,LA_TIMER,LA_TIMER,GetTickCount()) );
}
break;
case WM_TIMER:
{

switch(LOWORD(wParam)) {

case LA_TIMER:
{ // I cant turn the fricken timer off :(

int success = KillTimer(hwnd,LA_TIMER);
if (success == 0) {
MessageBox(hwnd,"Timer NOT killed","",MB_OK);
}
else MessageBox(hwnd,"Timer killed","",MB_OK);
// KillTimer(hwnd,ID); doesn't work
//break;
}
break;
default:
break;
}
}
break;
default:
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}



int WINAPI WinMain(HINSTANCE gInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = gInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(DKGRAY_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "Custom Class";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// if registration of main class fails
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"Custom Class",
"Laser Example",
WS_CAPTION|WS_MINIMIZEBOX|WS_VISIBLE|WS_OVERLAPPED|WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 500,
NULL, NULL, gInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}



Share this post


Link to post
Share on other sites
Oh, I think I see what happened. You had to call killtimer before the messagebox is all.
Glad it works though!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement