Results 1 to 22 of 22

Thread: Timers in console ?

  1. #1

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362

    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 ?

  2. #2

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Can we use TIMER in console application?
    The answer is boolean, yes or no

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    timeSetEvent...hummm I will read about it in MSDN help, thx

  5. #5

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    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?

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Did you #include <windows.h> before mmsystem.h?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    but i thought all windows functions were included in windows.h....

    guess not
    retired member. Thanks for everything

  8. #8

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    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.

  9. #9
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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.
    Baaaaaaaaah

  10. #10

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    I do not understand why I have to do that?

  11. #11
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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.
    Baaaaaaaaah

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The function needs to have the __stdcall attribute, as the error said
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Member Stef's Avatar
    Join Date
    May 2001
    Posts
    41
    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>
    Last edited by Stef; Aug 19th, 2002 at 08:11 AM.

  14. #14

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    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.

  15. #15
    Member Stef's Avatar
    Join Date
    May 2001
    Posts
    41

    Lightbulb

    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

  16. #16

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362

    Back to my old name!

    THX! I will check that code later, and check about CALLBACK, I never used that word before


  17. #17

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    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

  18. #18
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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.
    Baaaaaaaaah

  19. #19

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    I have windows.h, I copy and paste Stef code's

  20. #20
    Member Stef's Avatar
    Join Date
    May 2001
    Posts
    41
    Ah, sorry, forgot to tell you about that.
    - 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"

  21. #21
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  22. #22

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    I have lost the link to that thread and I just tested it and it work!

    Thx all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width