Q: How to set Sleep() between Sleep(0) and Sleep(1)? 
Author Message
 Q: How to set Sleep() between Sleep(0) and Sleep(1)?

Hello everyone,

I'm doing some animation with about 1000 frames.

  for (int i = 0; i < 1000; i)
  {
    // show frame[i]
  }

It works fine except it is too fast. So I changed it to

  for (int i = 0; i < 1000; i)
  {
    // show frame[i]
    ::Sleep(0);
  }

which doesn't have any effect except it gets along with other threads.
Then I changed it to:

  for (int i = 0; i < 1000; i)
  {
    // show frame[i]
    ::Sleep(1);
  }

This time, it was too slow. And finally

  for (int i = 0; i < 1000; i)
  {
    // show frame[i]
    if (i % 5 == 0)
      ::Sleep(1);
  }

It works most of the time, but on some machines, it's either too fast
or too slow. I think it has something to do with the installed video
cards and/or computers' speed. Anyhow, what would be an effective way
to control the animation speed?
__
ashier



Tue, 12 Jun 2001 03:00:00 GMT  
 Q: How to set Sleep() between Sleep(0) and Sleep(1)?
Ashier,

Try using this Wait() function instead of Sleep().

void CMyApp::Wait
  (DWORD dwInterval)  // time interval in mSec
//
//  Purpose:
//    Attempts to pause the current thread for a specific time interval.
//    Note, it's possible that the thread may pause for a longer
interval
//    if the system tick count rolls over during the pause.
//
//  Returns:
//    None
//
{
DWORD   dwTickEnd;      // end tick count
DWORD   dwTickStart;    // start tick count

  dwTickStart = ::GetTickCount();
  do
  {
    dwTickEnd = ::GetTickCount();
    if (dwTickEnd < dwTickStart)
       dwTickEnd = dwTickStart;
  }
  while (dwInterval > (dwTickEnd - dwTickStart));

Quote:
}

Also see  http://codeguru.com/misc/HighResolutionTimer.shtml for info on
how to cook up a high-resolution timer.

/ravi

"There is always one more bug..."

http://members.xoom.com/ravib

--------------------

Quote:

> Hello everyone,

> I'm doing some animation with about 1000 frames.

>   for (int i = 0; i < 1000; i)
>   {
>     // show frame[i]
>   }

> It works fine except it is too fast. So I changed it to

>   for (int i = 0; i < 1000; i)
>   {
>     // show frame[i]
>     ::Sleep(0);
>   }

> which doesn't have any effect except it gets along with other threads.
> Then I changed it to:

>   for (int i = 0; i < 1000; i)
>   {
>     // show frame[i]
>     ::Sleep(1);
>   }

> This time, it was too slow. And finally

>   for (int i = 0; i < 1000; i)
>   {
>     // show frame[i]
>     if (i % 5 == 0)
>       ::Sleep(1);
>   }

> It works most of the time, but on some machines, it's either too fast
> or too slow. I think it has something to do with the installed video
> cards and/or computers' speed. Anyhow, what would be an effective way
> to control the animation speed?
> __
> ashier

--------------------


Tue, 12 Jun 2001 03:00:00 GMT  
 Q: How to set Sleep() between Sleep(0) and Sleep(1)?


Quote:
>Try using this Wait() function instead of Sleep().

>void CMyApp::Wait
>  (DWORD dwInterval)  // time interval in mSec
>//
>//  Purpose:
>//    Attempts to pause the current thread for a specific time interval.
>//    Note, it's possible that the thread may pause for a longer
>interval
>//    if the system tick count rolls over during the pause.
>//
>//  Returns:
>//    None
>//
>{
>DWORD   dwTickEnd;      // end tick count
>DWORD   dwTickStart;    // start tick count

>  dwTickStart = ::GetTickCount();
>  do
>  {
>    dwTickEnd = ::GetTickCount();
>    if (dwTickEnd < dwTickStart)
>       dwTickEnd = dwTickStart;
>  }
>  while (dwInterval > (dwTickEnd - dwTickStart));
>}

I dunno, but since the mininum value for this Wait() function is 1
millisecond and ::Sleep(1) also sleeps for 1 millisecond, aren't they
essentially the same thing?
__
ashier


Wed, 13 Jun 2001 03:00:00 GMT  
 Q: How to set Sleep() between Sleep(0) and Sleep(1)?

Quote:



>>Try using this Wait() function instead of Sleep().

>>void CMyApp::Wait
>>  (DWORD dwInterval)  // time interval in mSec
>>//
>>//  Purpose:
>>//    Attempts to pause the current thread for a specific time interval.
>>//    Note, it's possible that the thread may pause for a longer
>>interval
>>//    if the system tick count rolls over during the pause.
>>//
>>//  Returns:
>>//    None
>>//
>>{
>>DWORD   dwTickEnd;      // end tick count
>>DWORD   dwTickStart;    // start tick count

>>  dwTickStart = ::GetTickCount();
>>  do
>>  {
>>    dwTickEnd = ::GetTickCount();
>>    if (dwTickEnd < dwTickStart)
>>       dwTickEnd = dwTickStart;
>>  }
>>  while (dwInterval > (dwTickEnd - dwTickStart));
>>}

>I dunno, but since the mininum value for this Wait() function is 1
>millisecond and ::Sleep(1) also sleeps for 1 millisecond, aren't they
>essentially the same thing?

No. Sleep() puts your thread into an efficient wait state. The function
above busy waits and is anathema under a multitasking OS. Note that the
resolution of the functions in question is likely nowhere near 1 msec. The
GetTickCount documentation says:

"It is limited to the resolution of the system timer. If you need a higher
resolution timer, use a multimedia timer or a high-resolution timer. "

--
Doug Harrison



Wed, 13 Jun 2001 03:00:00 GMT  
 Q: How to set Sleep() between Sleep(0) and Sleep(1)?


Fri, 19 Jun 1992 00:00:00 GMT  
 Q: How to set Sleep() between Sleep(0) and Sleep(1)?
Take a look at timeBeginPeriod() and timeEndPeriod() along with
timeGetDevCaps().  With these functions you can set the minimum resolution
for Sleep() and some other timer funtions from the default of 10 miliseconds
to 1 milisecond on an Intel PC.

What is happening now is that if you specify anything from 1ms to 10ms you
will get 10ms.

Specifying Sleep(0) does not guarantee any time will be spent away from your
program other than the time it take to check if a thread of equal priority
is waiting.

Quote:

>Hello everyone,

>I'm doing some animation with about 1000 frames.

>  for (int i = 0; i < 1000; i)
>  {
>    // show frame[i]
>  }

>It works fine except it is too fast. So I changed it to

>  for (int i = 0; i < 1000; i)
>  {
>    // show frame[i]
>    ::Sleep(0);
>  }

>which doesn't have any effect except it gets along with other threads.
>Then I changed it to:

>  for (int i = 0; i < 1000; i)
>  {
>    // show frame[i]
>    ::Sleep(1);
>  }

>This time, it was too slow. And finally

>  for (int i = 0; i < 1000; i)
>  {
>    // show frame[i]
>    if (i % 5 == 0)
>      ::Sleep(1);
>  }

>It works most of the time, but on some machines, it's either too fast
>or too slow. I think it has something to do with the installed video
>cards and/or computers' speed. Anyhow, what would be an effective way
>to control the animation speed?
>__
>ashier



Fri, 15 Jun 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. interrupt sleep() or successive sleeps

2. To sleep or not to sleep, THAT is the question!

3. Wake Sleeping Thread?

4. sleep

5. Sleep (or Delay) without Timer

6. Restarting a sleeping thread

7. Sleep function

8. Thread.Sleep

9. Sleep after kill() self?

10. Sleep

11. sleeping with the enemy? J2EE and .NET

12. sleep

 

 
Powered by phpBB® Forum Software