Valhalla Legends Forums Archive | C/C++ Programming | SetTimer()?

AuthorMessageTime
Eli_1
I remember using SetTimer with VB but I don't know how to do it with C++. I tryed researching it for about an hour but everything I found uses CWnd:: which wasn't working for me. if anyone knows how I can use the SetTimer() and KillTimer() APIs with c++ please tell me.
February 8, 2004, 5:13 AM
Adron
You were previously making a console app - so I just wanted to warn you that SetTimer and KillTimer aren't the best for console apps.

From MSDN:
[quote]
[code]
UINT_PTR SetTimer(
HWND hWnd, // handle to window
UINT_PTR nIDEvent, // timer identifier
UINT uElapse, // time-out value
TIMERPROC lpTimerFunc // timer procedure
);
[/code]
[/quote]

You have two choices:

* Call SetTimer with the handle to your window, an (nonzero) identifier of your choice and the timeout value, then write a handler for WM_TIMER in your WindowProc. You will get that message when the timeout elapses.

* Call SetTimer with the pointer to a function you've written and the timeout value. You still need to be processing window messages or it won't work. Your function will be called when the timeout elapses.

In both cases, unused arguments should be zero.

To abort the timer, you call KillTimer with your window + identifier in the first case, or with the return value from the SetTimer call in the second case.[code][/code]
February 8, 2004, 11:16 AM
Eli_1
[quote author=Adron link=board=30;threadid=5166;start=0#msg43100 date=1076238981]
* Call SetTimer with the handle to your window, an (nonzero) identifier of your choice and the timeout value, then write a handler for WM_TIMER in your WindowProc. You will get that message when the timeout elapses.
[/quote]

Here's the code I have for the timer.
[code]
#include <windows.h> // for windowproc??
#include <winuser.h> // same??
#include <iostream.h>
#include <stdlib.h>

HWND hwnd;
// WNDCLASSEX wc;
int my_timer;

int main() {
   bool nostop=true;
   char input[256];
   
   hwnd=GetForegroundWindow();

   // wc.lpfnWndProc = WindowProc;
   my_timer=SetTimer(hwnd, 1, 1000, NULL);
   cout << "Timer created. hWnd->" << hwnd << "\n";
   while (nostop==true) { continue; }
   KillTimer(hwnd,my_timer);
   return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
   cout << "Timer! - uMsg->" << uMsg << "\n";
}
[/code]
Output:
[code]
Timer created! hWnd->00000A70
[/code]
The problem is WindowProc is never being called? any ideas?
February 8, 2004, 6:02 PM
iago
Like Adron said, you have to be processing messages for it to work. In a console ap, you generally dont process messages, so it might be a lot trickier.

On a side note, that "continue;" in your code was completely unnecessary. When it gets to the end up a loop, it will continue anyway. A sleep(0) might be better there
February 8, 2004, 6:42 PM
Maddox
[quote author=Eli_1 link=board=30;threadid=5166;start=0#msg43138 date=1076263324]
The problem is WindowProc is never being called? any ideas?
[/quote]

The reason is this is happening is because you've left the lpTimerFunc argument NULL and your TIMERPROC function's declaration is incorrect.

Try this
[code]
// this should changed in main()
my_timer=SetTimer(hwnd, 1, 1000, MyTimer);


// timer proc
VOID CALLBACK MyTimer(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
cout << "Timer! - uMsg->" << uMsg << "\n";
}
[/code]

Hope that helps.
February 8, 2004, 8:16 PM
Eli_1
[quote]
error C2664: 'SetTimer' : cannot convert parameter 4 from 'void (struct HWND__ *,unsigned int,unsigned long,unsigned long)' to 'void (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,un
signed long)'
None of the functions with this name in scope match the target type
Error executing cl.exe.
[/quote]

Here's some of the code I'm using
[code]
//prototypes
VOID CALLBACK TimProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);


my_timer=SetTimer(hwnd,1,int_input,TimProc);



VOID CALLBACK TimProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
cout << "Timer! - uMsg->" << uMsg << "\n";
}
[/code]
February 8, 2004, 10:49 PM
iago
Read the error message for this one:
[quote]
error C2664: 'SetTimer' : cannot convert parameter 4 from
'void (struct HWND__ *,unsigned int,unsigned long,unsigned long)'
to
'void (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,unsigned long)'
[/quote]

Seems pretty obvious to me.
February 9, 2004, 12:39 AM
Eli_1
what's __stdcall*? ???
February 9, 2004, 12:55 AM
MrRaza
MSDN explains this all for you.
February 9, 2004, 1:13 AM
iago
Declare your function with _stdcall there.

void __stdcall myFunc(.....);

or, if it has to be a pointer, it's either
void *__stdcall myFunc(.....);
or
void __stdcall *myFunc(.....);
I'm not sure which is right, but I'm sure Skywing or Kp or somebody else will clear that up, or you can just try both.
February 9, 2004, 1:59 AM
Kp
[quote author=iago link=board=30;threadid=5166;start=0#msg43225 date=1076291966]
Declare your function with _stdcall there.

void __stdcall myFunc(.....);

or, if it has to be a pointer, it's either
void *__stdcall myFunc(.....);
or
void __stdcall *myFunc(.....);
I'm not sure which is right, but I'm sure Skywing or Kp or somebody else will clear that up, or you can just try both.[/quote]

The compiler should've picked up on that part automatically. If it didn't, then he should use &TimProc as the parameter to SetTimer (use an & to explicitly take its address). Regardless, he needs to add _stdcall to the function declaration (as you mentioned). I'm actually rather surprised to see that error, since iirc CALLBACK is aliased to _stdcall.
February 9, 2004, 2:39 AM
Adron
[quote author=Maddox link=board=30;threadid=5166;start=0#msg43168 date=1076271401]
[quote author=Eli_1 link=board=30;threadid=5166;start=0#msg43138 date=1076263324]
The problem is WindowProc is never being called? any ideas?
[/quote]

The reason is this is happening is because you've left the lpTimerFunc argument NULL and your TIMERPROC function's declaration is incorrect.

[/quote]

Like I said before, even with a timerproc, you have to process window messages. So it will never work as long as the program consists of an endless while loop doing nothing special. Making the while loop something like:

[code]
MSG msg;
while(GetMessage(&msg, 0, 0, 0) == TRUE)
DispatchMessage(&msg);
[/code]

should help things. In addition to that you should use either the timerproc way, or actually make a window for your windowproc.
February 9, 2004, 6:13 AM
Zakath
That error is puzzling. Kp was right about CALLBACK. See the MSVS tooltip when CALLBACK is pointed at?

[img]http://www.valhallalegends.com/zakath/CALLBACK.png[/img]
February 9, 2004, 3:41 PM
kamakazie
[quote author=Zakath link=board=30;threadid=5166;start=0#msg43300 date=1076341289]
That error is puzzling. Kp was right about CALLBACK. See the MSVS tooltip when CALLBACK is pointed at?

[img]http://www.valhallalegends.com/zakath/CALLBACK.png[/img]
[/quote]

Your answer lies in the declaration of the arguments. Notice how he declares the type of idEvent to UINT_PTR, it should be UINT. Even though MSDN documents it as UINT_PTR for TimerProc Function, in their Using Timers example it uses the UINT convention and this is the way it is specified in WinUser.h:
[code]
WINUSERAPI
UINT
WINAPI
SetTimer(
HWND hWnd ,
UINT nIDEvent,
UINT uElapse,
TIMERPROC lpTimerFunc);
[/code]
February 10, 2004, 5:36 AM

Search