-
Timers in console ?
I have read something in the HELP of MSDN and it said that it will add something in the main of the program like that :
Code:
case WM_TIMER:
switch (wParam)
{
case IDT_TIMER1:
...............
But is that only when it's not a console ? Cause I do not know where to add that code in a console ?
-
Can we use TIMER in console application?
The answer is boolean, yes or no :D
-
Just make life easy and use timeSetEvent, which is far more accurate, and it doesn't mess up the message queue (just calls your function when it times out).
-
timeSetEvent...hummm I will read about it in MSDN help, thx :)
-
Code:
#include <iostream>
#include <mmsystem.h>
using namespace std;
void Print(void);
void main()
{
timeSetEvent(
1000,
0,
Print(),
0,
0);
}//End main
void Print(void)
{
cout << "ForEVER" << endl;
}//End print
[error]
mainSource.cpp
c:\program files\microsoft visual studio\vc98\include\mmsystem.h(113) : error C2146: syntax error : missing ';' before identifier 'MMVERSION'
c:\program files\microsoft visual studio\vc98\include\mmsystem.h(113) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
[/error]
What do I do wrong?
-
Did you #include <windows.h> before mmsystem.h?
-
but i thought all windows functions were included in windows.h....
guess not :rolleyes:
-
OK now I have an other error :
Code:
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
using namespace std;
void Print(void);
void main()
{
timeSetEvent(
1000,
0,
Print(),
0,
0);
}//End main
void Print(void)
{
cout << "ForEVER" << endl;
}//End print
error C2664: 'timeSetEvent' : cannot convert parameter 3 from 'void' to 'void (__stdcall *)(unsigned int,unsigned int,unsigned long,unsigned l
ong,unsigned long)'
Expressions of type void cannot be converted to other types
Error executing cl.exe.
-
Try declaring Print() something like this:
PHP Code:
void Print(unsigned int something,unsigned int bah,unsigned long what,unsigned long hmm,unsigned long geeky);
Look at the reference for setTimeEvent to see what the parameters are.
-
I do not understand why I have to do that?
-
It said "cannot convert parameter 3 from 'void' to 'void (__stdcall *)(unsigned int,unsigned int,unsigned long,unsigned l
ong,unsigned long)" so it's probably not able to use the parameter void. Try it and see if it works.
-
The function needs to have the __stdcall attribute, as the error said ;)
-
I think the problem here is in the "()" after Print. C expects a pointer to a function, and the way you're doing this now is calling the function and passing the return value (a void) to timeSetEvent.
The following code might work:
Code:
timeSetEvent(
1000,
0,
Print,
0,
0);
Edit: Oh, and mmsystem.h is included in windows.h, so you only need to #include <windows.h> :)
-
PHP Code:
#include <iostream>
#include <windows.h>
//#include <mmsystem.h>
using namespace std;
void Print(void);
void main()
{
timeSetEvent(
1000,
0,
Print,
0,
0);
}//End main
void Print(void)
{
cout << "ForEVER" << endl;
}//End print
Code:
error C2664: 'timeSetEvent' : cannot convert parameter 3 from 'void (void)' to 'void (__stdcall *)(unsigned int,unsigned int,unsigned long,uns
igned long,unsigned long)'
None of the functions with this name in scope match the target type
Error executing cl.exe.
-
Ah, I got it to work now :)
For some reason timeSetEvent expects a callback function with a lot of parameters. I don't know what they are, but you could experiment with them if you want. Otherwise, just ignore them as I have done in the following code.
(The while() is just to make sure the program doesn't end before the timer is activated)
Code:
#include <iostream.h>
#include <windows.h>
//using namespace std;
int g_Stop = 0;
void CALLBACK Print(unsigned int,unsigned int,unsigned long,unsigned long,unsigned long)
{
cout << "ForEVER" << endl;
g_Stop = 1;
}//End print
int main()
{
timeSetEvent(
1000,
0,
Print,
0,
0);
while( g_Stop == 0 )
{
cout << ".";
}
return 0;
}//End main
-
Back to my old name!
THX! I will check that code later, and check about CALLBACK, I never used that word before :eek:
:)
-
Here is what I got :
Code:
Compiling...
mainSource.cpp
Linking...
mainSource.obj : error LNK2001: unresolved external symbol __imp__timeSetEvent@20
Debug/DosTimer.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
DosTimer.exe - 2 error(s), 0 warning(s)
Error are in the linkage:confused:
-
You probably forgot to include windows.h that's why you're getting the linking error.
I think CALLBACK is defined as _stdcall and setTimeEvent requires the function to be declared as such.
-
I have windows.h, I copy and paste Stef code's
-
Ah, sorry, forgot to tell you about that.:rolleyes:
- Go to Project > Settings
- Choose "All configurations" from the drop down menu (on the left)
- Go to the Link tab
- Add winmm.lib to the line labeled "Object/library modules"
-
If you have VC7 right-click your project in the solution explorer, choose properties, then select the project again if you have to, then go to linker-> input and add the library there
-
I have lost the link to that thread and I just tested it and it work!
Thx all :)