473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Accurate timer events w/ no window available.

Using C++, platform is Windows XP SP3.

I have a multimedia application that runs a rendering loop that must be throttled to 60 iterations / second max. Currently it is implemented using a dirty and inconsistent wait loop, which I've included below. I am looking for a better way to implement frame rate throttling, and am looking for a timed event that meets the following requirements:

* Does not require a window handle.
* Does not require a message processing loop.
* Minimum CPU usage is very important.
* Consistent, accurate to within 0.1 ms would be ideal.
* If loop time is greater than 1/60th of a second, it must still run the loop as fast as possible rather than skipping timeslices (e.g. run at 55fps instead of halving it to 30 with a lot of idle time).

The SetTimer interface requires a message processing loop, and is not appropriate for this application. It also has dubious accuracy. I have just recently discovered the MSHTML Timer API (http://msdn.microsoft.com/en-us/libr...TML_Timer_API), but have not tried using it yet. However, it appears that that also required a window handle to be available, but maybe I'm wrong?

Current implementation, does not meet accuracy / consistency requirements in certain situations. The idea is to use the performance counter and wait in a loop, but use Sleep() with various parameters to minimize CPU usage (it is expected that Sleep(1) takes significantly longer than 1 ms), depending on the amount of time left to spare:
// vars used below, for context
LARGE_INTEGER tn; // current time
LARGE_INTEGER tl; // last frame
LARGE_INTEGER tfreq; // counter freq
// 1.0 / max frame rate (in this
// case, 1.0/60.0=0.016)
double tminftime;

// initial values of vars
// used below
tminftime = 1.0 / 60.0;
QueryPerformanceFrequency(&tfreq);
QueryPerformanceCounter(&tl);

while (...) {
renderFrame();
// throttle frame rate; pause
// for remainder of 1/60th
// second time slice
while (true) {
QueryPerformanceCounter(&tn);
double realdt = (double)(tn.QuadPart - tl.QuadPart) / (double)tfreq.QuadPart;
// if not enough time passed,
// then wait.
if (realdt < tminftime) {
double timeleft =
tminftime - realdt;
if (timeleft 0.011)
Sleep(1); // be nicer
else if (timeleft 0.001)
Sleep(0); // be nice
// if < 1ms then dont be
// nice at all.
} else {
break;
}
}
tl = tn;
}
Thanks,
Jason
Oct 28 '08 #1
2 2144
JC
Apologies for cross-posting the message included below. I was having
issues with Google Groups and did not think the posting worked at all.
I also posted this same question on the MSDN forum and it was answered
there:

http://forums.microsoft.com/MSDN/Sho...53852&SiteID=1

Thanks,
Jason
On Oct 27, 8:18*pm, JasonC wrote:
Using C++, platform is Windows XP SP3.

I have a multimedia application that runs a rendering loop that must be throttled to 60 iterations / second max. Currently it is implemented using adirty and inconsistent wait loop, which I've included below. I am looking for a better way to implement frame rate throttling, and am looking for a timed event that meets the following requirements:

** Does not require a window handle.
** Does not require a message processing loop.
** Minimum CPU usage is very important.
** Consistent, accurate to within 0.1 ms would be ideal.
** If loop time is greater than 1/60th of a second, it must still run the loop as fast as possible rather than skipping timeslices (e.g. run at 55fps instead of halving it to 30 with a lot of idle time).

The SetTimer interface requires a message processing loop, and is not appropriate for this application. It also has dubious accuracy. I have just recently discovered the MSHTML Timer API (http://msdn.microsoft.com/en-us/libr...x#The_MSHT...), but have not tried using it yet. However, it appears that that also required a window handle to be available, but maybe I'm wrong?

Current implementation, does not meet accuracy / consistency requirementsin certain situations. The idea is to use the performance counter and waitin a loop, but use Sleep() with various parameters to minimize CPU usage (it is expected that Sleep(1) takes significantly longer than 1 ms), depending on the amount of time left to spare:

* *// vars used below, for context
* *LARGE_INTEGER tn; // current time
* *LARGE_INTEGER tl; // last frame
* *LARGE_INTEGER tfreq; // counter freq
* *// 1.0 / max frame rate (in this
* *// case, 1.0/60.0=0.016)
* *double tminftime;

* *// initial values of vars
* *// used below
* *tminftime = 1.0 / 60.0;
* *QueryPerformanceFrequency(&tfreq);
* *QueryPerformanceCounter(&tl);

* *while (...) {
* * renderFrame();

* * // throttle frame rate; pause
* * // for remainder of 1/60th
* * // second time slice
* * while (true) {
* * *QueryPerformanceCounter(&tn);
* * *double realdt = (double)(tn.QuadPart - tl.QuadPart) / (double)tfreq.QuadPart;
* * *// if not enough time passed,
* * *// then wait.
* * *if (realdt < tminftime) {
* * * *double timeleft =
* * * * * * * *tminftime - realdt;
* * * *if (timeleft 0.011)
* * * * *Sleep(1); // be nicer
* * * *else if (timeleft 0.001)
* * * * *Sleep(0); // be nice
* * * *// if < 1ms then dont be
* * * *// nice at all.
* * *} else {
* * * *break;
* * *}
* * }
* * tl = tn;

* *}

Thanks,
Jason
Oct 28 '08 #2
JC wrote:
Apologies for cross-posting the message included below. I was having
issues with Google Groups and did not think the posting worked at all.
I also posted this same question on the MSDN forum and it was answered
there:

http://forums.microsoft.com/MSDN/Sho...53852&SiteID=1
Posted an additional reply on the forum.
>
Thanks,
Jason
On Oct 27, 8:18 pm, JasonC wrote:
>Using C++, platform is Windows XP SP3.

I have a multimedia application that runs a rendering loop that must
be throttled to 60 iterations / second max. Currently it is
implemented using a dirty and inconsistent wait loop, which I've
included below. I am looking for a better way to implement frame
rate throttling, and am looking for a timed event that meets the
following requirements:

* Does not require a window handle.
* Does not require a message processing loop.
* Minimum CPU usage is very important.
* Consistent, accurate to within 0.1 ms would be ideal.
* If loop time is greater than 1/60th of a second, it must still run
the loop as fast as possible rather than skipping timeslices (e.g.
run at 55fps instead of halving it to 30 with a lot of idle time).

The SetTimer interface requires a message processing loop, and is
not appropriate for this application. It also has dubious accuracy.
I have just recently discovered the MSHTML Timer API
(http://msdn.microsoft.com/en-us/libr...x#The_MSHT...),
but have not tried using it yet. However, it appears that that also
required a window handle to be available, but maybe I'm wrong?

Current implementation, does not meet accuracy / consistency
requirements in certain situations. The idea is to use the
performance counter and wait in a loop, but use Sleep() with various
parameters to minimize CPU usage (it is expected that Sleep(1) takes
significantly longer than 1 ms), depending on the amount of time
left to spare:

// vars used below, for context
LARGE_INTEGER tn; // current time
LARGE_INTEGER tl; // last frame
LARGE_INTEGER tfreq; // counter freq
// 1.0 / max frame rate (in this
// case, 1.0/60.0=0.016)
double tminftime;

// initial values of vars
// used below
tminftime = 1.0 / 60.0;
QueryPerformanceFrequency(&tfreq);
QueryPerformanceCounter(&tl);

while (...) {
renderFrame();

// throttle frame rate; pause
// for remainder of 1/60th
// second time slice
while (true) {
QueryPerformanceCounter(&tn);
double realdt = (double)(tn.QuadPart - tl.QuadPart) /
(double)tfreq.QuadPart; // if not enough time passed,
// then wait.
if (realdt < tminftime) {
double timeleft =
tminftime - realdt;
if (timeleft 0.011)
Sleep(1); // be nicer
else if (timeleft 0.001)
Sleep(0); // be nice
// if < 1ms then dont be
// nice at all.
} else {
break;
}
}
tl = tn;

}

Thanks,
Jason

Nov 5 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
11
by: Steve Jorgensen | last post by:
I've recently been playing with some UI ideas that require the user of a timer to drive animation. The problem I'm having is that Access routinely stops firing timer events for long periods of...
5
by: theinvisibleGhost | last post by:
I'm having a problem that occurs at random in my app. I get an exception "Cannot Access a disposed object" In MSCorLib when calling boolean Change (int32, int32) Stack trace reveals...
12
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown...
2
by: r norman | last post by:
Please excuse the cross-posting. This question was raised in microsoft.public.dotnet.general but hasn't been answered so I am trying where I can. There are two of us who have the same problem...
4
by: Ben | last post by:
Hello everybody I got confused by this problem for which I don't have a logical explanation. There is a Thread (ThreadA) which receives Events from another system thread (ThreadS). ThreadA then...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
4
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this...
11
by: Hotrod2000 | last post by:
I'm quite new to programming but I'm having problems getting a timer to work in visual studio.net I've created a timer on a form, enabled it and then typed the following code (from the mdsn...
0
by: Jason C | last post by:
Using C++, platform is Windows XP SP3. I have a multimedia application that runs a rendering loop that must be throttled to 60 iterations / second max. Currently it is implemented using a dirty...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.