Thread: Timer

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    Timer

    Hi!
    I'm having some trouble with creating a timer (for the 1st time) for my win32 dialog app. I tried to call SetTimer() from the dialog procedure, and created a Timer procedure for it. But it didn't work. The breakpoint inside TimerProc() never got hit. What i'm doing wrong, please?

    Code:
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
       DialogBox(hInstance, MAKEINTRESOURCE(IDMAINDLG), hWnd, 
    			reinterpret_cast<DLGPROC>(DlgProc));
       ...............
       ...............
    }
    
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg,
    			WPARAM wParam, LPARAM lParam)
    {
       ...............
       ...............
    
       SetTimer(hWndDlg,IDTIMER1,1000,(TIMERPROC)TimerProc);
    
    }
    
    VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,
    		UINT_PTR idEvent,DWORD dwTime){	
    
       ...............
       ...............
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Try this
    Code:
    SetTimer(hWndDlg,IDTIMER1,1000,(TIMERPROC)&TimerProc  c);

  3. #3
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    Sorry it doesn't seems to work.
    I also tried to call SetTimer() within WinMain(), and placed a WN_TIMER event handler in DlgProc(). But no luck either.

    Code:
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
       LPSTR lpCmdLine, int nCmdShow)
    {
       HWND Dlg;
       SetTimer(Dlg,IDTIMER1,100,NULL);
      ..........
     }
    
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg,
    WPARAM wParam, LPARAM lParam)
    {
       ...............
       switch(Msg){
       case WM_TIMER:
         switch(wParam){
         case IDTIMER1:
            .....
            .....
         }
         .......
       }
    }
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    This should work for you

    Wherever you declare your globals:
    Code:
    #define IDC_TIMER 600 /* Your choice for ID */
    UINT g_unTimerRet;
    In your message callback:
    Code:
    LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
         switch (message) {
              case WM_CREATE: {
                   g_unTimerRet = SetTimer(hWnd, IDC_TIMER, 1000, &TimerProc);
                   break;
              }
              case WM_DESTROY: {
                   KillTimer(hWnd, g_unTimerRet); /* Don't forget to cleanup your mess! */
                   break;
              }
    Declare your callback for the timer (prototype) wherever you plan on putting the whole function. For example in main.c:
    Code:
    /* Prototype */
    void CALLBACK TimerProc(HWND, UINT, UINT, DWORD);
    
    /* The timer function that is executed every second according to when it was created */
    void CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime) {
         /* Whatever code you want executed here */
    }
    I know this works because I use it in my apps and I acquired it from MSDN.

  5. #5
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    Thanx.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM
  4. Timer again.
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-04-2005, 10:19 PM