Article View: comp.windows.ms.programmer
Article #732Re: Using a timer to "sleep"
From: David D'SOUZA
Date: Mon, 26 Nov 1990 06:26
Date: Mon, 26 Nov 1990 06:26
31 lines
1820 bytes
1820 bytes
In article <13796@june.cs.washington.edu> goble@wolf.cs.washington.edu
(Brian Goble) writes:
>What I would like to do is use this TimerCounter value to be able to
>do delays like when I display a message or something. I tried something
>like:
>
> TimerCounter = 0;
> while (TimerCounter < 5) { TimerCounter = TimerCounter; /* nothing */
>
>but it just spins in the loop. Does the timer callback function not
>interrupt the current execution? Is there a windows function or an MS C
>function (like Turbo C's "sleep()") that will do a delay?
This won't work as you found out... Windows timers are synchronous
beasts, and in fact, can only be triggered when you are calling
GetMessage. Another problem is Windows applications aren't
preemptively multitasked so you want to be careful about busy waiting
because you are starving other applications of processor time.
Here is a quick and dirty way to do a nice delay loop. Note, you also
need to becareful about GetTickCount wrapping back to zero... The
Yield() gives time to other applications. Note that if you are
running in REAL mode, when you Yield, your DS can move so be sure you
don't have any far pointers into your ds. (Check out LockSegment(-1) if
this is a problem.)
DWORD tickCount;
tickCount = GetTickCount();
while (ABSOLUTEVALUE(GetTickCount() - tickCount) < MYDELAYTIME)
Yield();
Technical Details
Message-ID:
<59287@microsoft.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!microsoft!daviddsReferences:
<13796@june.cs.washington.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!microsoft!davidds From: davidds@microsoft.UUCP (David D'SOUZA) Newsgroups: comp.windows.ms.programmer Subject: Re: Using a timer to "sleep" Message-ID: <59287@microsoft.UUCP> Date: 26 Nov 90 06:26:14 GMT References: <13796@june.cs.washington.edu> Reply-To: davidds@microsoft.UUCP (David D'SOUZA) Organization: Microsoft Corp., Redmond WA Lines: 31