.Net DateTime.Now not frequent enough?

  • Thread starter Thread starter Gareth
  • Start date Start date
G

Gareth

I am trying to use DateTime.Now to apply a frame limit to my 3d
application. Below is the code involved. In this example I have hard
coded it for 60 frames per second (1000 milliseconds / 60 fps).

public void Execute()
{
while(!_close)
{
DateTime n=DateTime.Now;
TimeSpan t=n.Subtract(_lastRender);

if(t.TotalMilliseconds>=1000.0d/60.0d)
{
_lastRender=n;
_render();
}
}
}

The problem is I always get 33 frames per second. If I remove the
frame limiter I get 2000+ frames per second. If I log the value of
t.TotalMilliseconds to a text file every iteration I get the
following:

Notice that TotalMilliseconds doesn't change at all for several
iterations, then suddenly doubles...

20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 15.6279
20/09/2007 09:50:11: 31.2558
20/09/2007 09:50:11: 0
20/09/2007 09:50:11: 0
20/09/2007 09:50:11: 0
20/09/2007 09:50:11: 0
 
Hello,
you should probably use the StopWatch instead (if you are using .NET 2.0),
which uses a high-resolution timer.

With .NET 1.1, you can still interop into Win32 QueryPerformanceCounter and
QueryPerformanceFrequency functions.

Kind regards,
Henning Krause
 
Hello,
you should probably use the StopWatch instead (if you are using .NET 2.0),
which uses a high-resolution timer.

With .NET 1.1, you can still interop into Win32 QueryPerformanceCounter and
QueryPerformanceFrequency functions.

Kind regards,
Henning Krause

Thanks, using QueryPerformanceCounter/Frequency has done the job!

A nice smooth 60fps :D

G
 
Back
Top