#### Archived

This topic is now archived and is closed to further replies.

# Is my code correct?

This topic is 5682 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I wrote some code wich I cannot test myself, the reason is, I can just let my PC running for 49.7 days. So, I want to post it here so you guys can tell me if it''s ok: This is part of a timer class, I want to check in particulair the code underneath the part Timer wrapped around:

bool cTimer::Check()
{
DWORD ulCurrentTime = GetTickCount();

if (bRunning)
{
// Timer wrapped around

if (ulCurrentTime < ulStartTime)
{
DWORD ulPassed = ULONG_MAX - ulStartTime + ulCurrentTime;
if (ulLimit < ulPassed)
{
bRunning = false;
return (true);
}

// Timer isn''t done yet

return (false);
}
else
{
if (ulStartTime + ulLimit < ulCurrentTime)
{
bRunning = false;
return (true);
}

// Timer isn''t done yet

return (false);
}
}

// Timer is not running, return true

return (true);
}

---------------- -Earth is 98% full. Please delete anybody you can.
My Site

##### Share on other sites
Ahem.... debugger? I''d trust what I saw in a debugger over what people said after reading the code, any day.

(step past GetTickCount, change ulCurrentTime, see what happens)

Or maybe I''m missing something obvious?

##### Share on other sites
You''re missing something obvious. According to the MSDN, GetTickCount() wraps around EVERY 49.7 days. So, it goes back to 0 every 50 days. However, I can''t just test this by waiting 49.7 days and then ending up in being not @ my PC when the wrap around happens. So, my question was if anyone is seeing a logical error in there.

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.

My Site

##### Share on other sites
And what I meant was, use a debugger to set the variable you copy the result from GetTickCount into, and see for yourself what happens. If there is a logic error, it should show itself.

##### Share on other sites
If your computer is running windows do you expect it to still be running in 40+ days?

##### Share on other sites
AP: Not mine, but there are pc's out there running windows be have an uptime of more then 50 days. Programming isn't just making things work, it's also about making sure possible pieces of code keep working under certain circumstances(sp?). It's like programming a game and thinking that everyone has working sound, there are still pcs out there with no sound.

[EDIT]
It seems like the MSDN is mistaken, GetTickCount wraps around every midnight(It seems, since the value 3xxxxxxxxxx, same length as ULONG_MAX). So, this is a rather delicate in wich the programmer could mess up pretty hard if not taken into consideration. So, I tested stuff and it seems like my code is correct.
[/EDIT]

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.

My Site

[edited by - Sand_Hawk on December 3, 2002 4:33:22 PM]

##### Share on other sites
Quick check on my system using GetTickCount says it''s been up 6 days, 5 hours, 46 minutes and some seconds (tick 539174732), so my box at least doesn''t roll around at midnight. That''s on win2k. Perhaps GetTickCount behavior varies with platform, I dunno (no mention in any MSDN source I checked, incl www version).

(reservation for sloppy math with the ticks but the result seems correct, I think I reset my machine a few days back).

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631428
• Total Posts
3000027
×