Thread: Synchronise WM_TIMERs, we're going in...

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Question Synchronise WM_TIMERs, we're going in...

    Hello,

    I've made a little program that changes the desktop wallpaper every hour. The only thing is, I want the code that changes it to be executed on the hour (1:00 PM, 2:00 PM, 3:00 PM). Since WM_TIMERs have a maximum elapse time of 65,535 milliseconds I'm running my function every minute, or 60,000 milliseconds, and checking if the number of minutes returned by GetSystemTime is zero. I can't help thinking that's just a smidge wasteful.

    Also, at the beginning of the program, I'm synchronising the timer used with the system clock like so:-
    Code:
    case WM_CREATE:
    {
    	SYSTEMTIME st;
    
    	PostMessage(hwnd, WM_TIMER, 0, 0); // run the function at startup
    	GetSystemTime(&st);
    	while (st.wSecond != 0) // Synchronised?
    	{
    		GetSystemTime(&st);
    		Sleep(1000); // Fudge fudge fudge
    	}
    
    	SetTimer(hwnd, TIMER, 60000, NULL);
    	break;
    }
    As I've pointed out, the Sleep function is perhaps the worst solution, ever, for this situation as it renders the earlier PostMessage call useless (It holds up the message pump for anything up to a minute). Please God (*cough*) I mean you, Is there a better way?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Best bet - use the OS......the task scheduler can be set to run your app at a certian interval.........but you will need COM..

    I'll try code an example later if I have time

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Originally posted by Fordy
    but you will need COM..
    You're really trying to hurt my feelings, aren't you? Break the chains, man! Reject the fascist object frameworks and be free!

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by SMurf
    You're really trying to hurt my feelings, aren't you? Break the chains, man! Reject the fascist object frameworks and be free!
    Pah!

    Anyway...I just realised that tasks run daily...............so for hourly checks they arent too good..

    Ok....revisiting this.....how about you create a worker thread to check the time and then call Sleep on that thread if the time isnt right......its a bit of a potch......but it will allow your main thread to react to user intervention and not freeze on the screen

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I like the WM_TIMER solution. You have to remember you're running a very small amount of code to do the check every time it's fired. I wouldn't worry at all about one minute intervals.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by FillYourBrain
    I like the WM_TIMER solution. You have to remember you're running a very small amount of code to do the check every time it's fired. I wouldn't worry at all about one minute intervals.
    That's a good point....59 ignored messages per hour isnt really that wastefull

  7. #7
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    One lightning strike later, and I had an epiphany: What if, right, just what-if, I was to create a second timer which is started first and queries the system time every second until its synchronised then starts the original timer?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 Joints, how to Synchronise
    By Bobby230 in forum C Programming
    Replies: 1
    Last Post: 03-05-2006, 03:39 PM