Thread: Timer help

  1. #1
    george7378
    Guest

    Timer help

    Hello,

    I have a simple Win32 program which I want to add a timer to. The thing is, I don't know where I would put the SetTimer() line to make the program do the stuff after the IDT_TIMER in order for it to work - where abouts should I put the SetTimer() line?

    Code:
    #include <windows.h>
    #include <math.h>
    #include <cmath>
    #include <cstdio>
    #include "resource.h"
    
    BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	switch(Message)
    	{
    		case WM_COMMAND:
    
    			switch(LOWORD(wParam))
    			{
    
    			case IDT_TIMER:
    
                   ...Do things when timer ticks.
    
    				break;	
    
    			}
    		break;
    
    		case WM_CLOSE:
    			EndDialog(hwnd, 0);
    		    break;
    
    		default:
    			return FALSE;
    	}
    	return TRUE;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_CLOCK), NULL, ToolDlgProc);
    
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Common sense would suggest that SetTimer() be called in response to some defined event (for example, when the user presses a button labeled "Start Timer") or before entering the main event loop.

    Generally, the needs of your program would determine what is appropriate.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    george7378
    Guest
    Ideally, the timer would be set when the program is started, because I designed it as a clock which displays and updates the system time in some dialog boxes, so I want to create a timer which ticks every second, and when it does, updates the system time.

    Thanks.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Well, then. You have defined where you need to call SetTimer(). The first line of WinMain() would be an appropriate place.

    As to updating the system time in response to a timer .... I assume you are referring to updating a display of the system time. Updating the actual system time within a timer is a chicken and egg scenario.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by george7378 View Post
    Hello,

    I have a simple Win32 program which I want to add a timer to. The thing is, I don't know where I would put the SetTimer() line to make the program do the stuff after the IDT_TIMER in order for it to work - where abouts should I put the SetTimer() line?

    Code:
    #include <windows.h>
    #include <math.h>
    #include <cmath>
    #include <cstdio>
    #include "resource.h"
    
    BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	switch(Message)
    	{
    		case WM_COMMAND:
    
    			switch(LOWORD(wParam))
    			{
    
    			case IDT_TIMER:
    
                   ...Do things when timer ticks.
    
    				break;	
    
    			}
    		break;
    
    		case WM_CLOSE:
    			EndDialog(hwnd, 0);
    		    break;
    
    		default:
    			return FALSE;
    	}
    	return TRUE;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_CLOCK), NULL, ToolDlgProc);
    
    }
    Thanks.
    In this code the best place to start the timer is in response to the WM_INITDIALOG message posted to the message loop when the dialog is created.

    Then to handle your timer events you would use WM_TIMER messages issued by the timers to call a function to update your clock display.

    The place to kill your timers is inresponse to the WM_CLOSE message.



    I noted that you want to tick it once a second... try twice or three times a second as any minor inaccuracies will be less apparent to the viewer.

    Lots of good information about timers --> here

  6. #6
    george7378
    Guest
    Code:
    #include <windows.h>
    #include <math.h>
    #include <cmath>
    #include <cstdio>
    #include "resource.h" 
    
    BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	switch(Message)
    	{
    
    	case WM_INITDIALOG:
            SetTimer(hwnd, IDT_TIMERCLOCK, 500, NULL);
    		return TRUE;
    		break;
    
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    			{
    			}
    		break;
    
    	case WM_TIMER:
    			{
          Get the time and date, display.
    
    				break;	}
    
    		case WM_CLOSE:
    			EndDialog(hwnd, 0);
    		    break;
    
    		default:
    			return FALSE;
    	}
    	return TRUE;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_CLOCK), NULL, ToolDlgProc);
    }
    Thanks a lot for that guys, the above code works beautifully.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Not with the current line 25 it doesn't....

  8. #8
    george7378
    Guest
    Oh yes, I have some code in there rather than just words. There is also a header and resource file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timer in c
    By cable in forum C Programming
    Replies: 1
    Last Post: 11-25-2010, 04:45 PM
  2. help - timer in C?
    By psu_ece in forum C Programming
    Replies: 6
    Last Post: 05-15-2010, 01:09 PM
  3. Timer
    By geek@02 in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2005, 11:20 AM
  4. need help with a timer
    By revelation437 in forum C++ Programming
    Replies: 10
    Last Post: 03-27-2003, 11:25 AM
  5. how to set timer
    By marianne in forum C Programming
    Replies: 1
    Last Post: 02-12-2003, 10:21 PM