Click to See Complete Forum and Search --> : SetTimer


UnfitElf
January 24th, 2006, 12:39 AM
Hi ppl.

I did search for this problem and i couldnt find one like it. If i overlooked it sorry.

My problem is when using SetTimer() in a function it doesnt work, yet when its not in the function it does work..

The following code will help you to see the strange probelm.

This does not work
HWND hwnd; // Global handle for our window

void timer(void)
{
SetTimer(hwnd, 1, 5000, (TIMERPROC)NULL);
}

// setting up the window etc.. has been omitted for ease of posting code

switch (message) /* handle the messages */
{
case WM_TIMER:
MessageBox(NULL, "Timer", "Timer", MB_OK);
break;
case WM_CREATE:
timer();
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

This Does work
HWND hwnd; // Global handle for our window

// setting up the window etc.. has been omitted for ease of posting code

switch (message) /* handle the messages */
{
case WM_TIMER:
MessageBox(NULL, "Timer", "Timer", MB_OK);
break;
case WM_CREATE:
SetTimer(hwnd, 1, 5000, (TIMERPROC)NULL);
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

I cannot understand this at all, any help?
Thanks

leojose
January 24th, 2006, 01:13 AM
Try checking...
1) The return value of SetTimer()
2) If the return value is 0(indicating the function failed) , then see what GetLastError() gives.
I believe you might find the answer there.

UnfitElf
January 24th, 2006, 01:29 AM
Hi, thanks for the reply.

Yes sorry i have aready done that however SetTimer does not return an 0.

NoHero
January 24th, 2006, 02:27 AM
case WM_CREATE:
SetTimer(hwnd, 1, 5000, (TIMERPROC)NULL);
break;

Maybe the local hwnd parameter of your dialog procedure overwrites the global one. May that be? ;)