Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

.Net DateTime.Now not frequent enough?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gareth - 20 Sep 2007 09:48 GMT
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
Henning Krause [MVP - Exchange] - 20 Sep 2007 11:00 GMT
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

>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
[quoted text clipped - 77 lines]
> 20/09/2007 09:50:11: 0
> 20/09/2007 09:50:11: 0
Gareth - 20 Sep 2007 14:07 GMT
> Hello,
> you should probably use the StopWatch instead (if you are using .NET 2.0),
[quoted text clipped - 5 lines]
> Kind regards,
> Henning Krause

Thanks, using QueryPerformanceCounter/Frequency has done the job!

A nice smooth 60fps :D

G

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.