Click here to Skip to main content
15,922,696 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionSetTimer call in CWinThread Pin
kumar_subrahmanya7-Jul-06 1:48
kumar_subrahmanya7-Jul-06 1:48 
AnswerRe: SetTimer call in CWinThread Pin
David Crow7-Jul-06 2:55
David Crow7-Jul-06 2:55 
GeneralRe: SetTimer call in CWinThread Pin
kumar_subrahmanya9-Jul-06 20:19
kumar_subrahmanya9-Jul-06 20:19 
AnswerRe: SetTimer call in CWinThread Pin
Randor 7-Jul-06 4:04
professional Randor 7-Jul-06 4:04 
GeneralRe: SetTimer call in CWinThread Pin
kumar_subrahmanya9-Jul-06 20:20
kumar_subrahmanya9-Jul-06 20:20 
QuestionDelay of 250micro Sec Pin
Shiva Prasad7-Jul-06 1:33
Shiva Prasad7-Jul-06 1:33 
AnswerRe: Delay of 250micro Sec Pin
Chris Losinger7-Jul-06 1:43
professionalChris Losinger7-Jul-06 1:43 
AnswerRe: Delay of 250micro Sec Pin
Rilhas9-Jul-06 2:30
Rilhas9-Jul-06 2:30 
Hi,

I don't think you will be able to do this in Windows. This is because the Windows timers don't really provide such high resolution. With multimedia timers you can get about 1ms resolution, at best. Check out the timeBeginPeriod function.

If you are into developing hardware you might solve your problem by developing a microcontroller to connect to a serial port, and then user the received serial data as event triggers. With a serial data rate of 128000 bps and 10 bits per byte, you would get about 12800 bytes per second (78 us per byte). Nevertheless, no real guarantees of accurate timming exist, because Windows could always group several bytes together before generating an event to the application, especially if there are other processes running. (you could try a loopback serial cable to avoid developing hardware).

What you could do, using assembly language or C, is to take advantage of the fairly predictable execution rate of processors (after the startup stabilization and cache warming up) to estimate how many instructions execute within a given timer tick interval. For example, if 10ms elapse between timer ticks, and during that time your processor executes 3,000,000 for() loops, then it is a reasonable approximation to consider that your processor will execute 300,000 instruction in 1 ms, or 75,000 in 250 us. So, to wait 250 us you would do "for(int i=0; i<75000; i++);". For this to be accurate your program should not be interrupted by other processes in the system (so it should have a high execution priority), and the for() loops you use to measure execution rate should be very similar (ideally the same) as the for() loops used for waiting. And, of course, the execution time wasted between waits should be negligeble.

One thing you must take into account when writting delays with this technique, is that the execution speed should be measured for each processor, or, ideally, before any execution. For the loops to be similar you could start a loop that you know will take about 1s or so to execute, and when it finished measure the time diference. For example:

void main(void) {
double t0, t1;
t0=((double)clock())/((double)CLOCKS_PER_SEC);
for(int i=0; i<500000000; i++);
t1=((double)clock())/((double)CLOCKS_PER_SEC);
printf("t1-t0=%g", t1-t0);
}

On my computer, which is a Pentium 4 running at 3.2 GHz, this 500,000,000 count loop takes about 1.1 seconds. If I run this application on my old 386 at 25 MHz I could expect it to take over 2 minutes, which could be unacceptable. If your application is to be run on such different machines you should consider first estimating the execution rate by using an alogorithm like:

1) Estimate how many loops can be executed in 0.5 seconds by using a while() loop that keeps running while the timer is under 0.5 seconds, while incrementing the loop counter.

2) Execute the real measurement for() loop using the estimated value in 1), corrected to execute in the time you think is acceptable, while measuring the start time and the end time. For example, if you think users accept waiting for 2 seconds while the application measures execution speed, then the real for() loop should run for a corrected value of about 3 times that achieved in the while loop (step 1) took 0.5 seconds, so step 2) should take no more than 1.5 seconds, or 3 times the time of setp1) ).

Since the loops are diferent, you should expect that a for() loop counting to a value 3 times higher than that achieved with the while() loop does not take 3 times as much time. But, since you measure the actual time of the for loop, that will not be very important for the accuracy of the measure.

For accuracy, the measurement loop should take enough time for the timer tick interval to be negligeable. For example, in Windows XP you get about 100 ticks per second, so measuring for 1 second gives you an error with an order of magnitude of about 1%. Executing measurements for 2 seconds 0.5%, and so on. The worst Windows timer resolution I know is with 95/98 with tick intervals of about 50 ms.

Also take into account that for the loops to be equal, the measure loop and the wait loop should use the same termination conditions. Either both use constant fixed limits, or both use variable limits, otherwise the compiler may generate different code for them. For the same reason, both should be subject to the same compiler optimizations. For best accuracy, try to isolate the loop in a function of its own.

An application built in this manner is not very user friendly, because it will be taking all the available CPU while waiting. This is especially noticeable if the process priority is high. With enough bad programming you can even make the CPU so busy with your loops that not even the mouse pointer gets any CPU to move around.



Rilhas
QuestionMulticolumn listbox Pin
Amit Agarrwal7-Jul-06 1:30
Amit Agarrwal7-Jul-06 1:30 
AnswerRe: Multicolumn listbox /*modified*/ Pin
Hamid_RT7-Jul-06 1:34
Hamid_RT7-Jul-06 1:34 
GeneralRe: Multicolumn listbox /*modified*/ Pin
Amit Agarrwal7-Jul-06 2:18
Amit Agarrwal7-Jul-06 2:18 
GeneralRe: Multicolumn listbox Pin
Hamid_RT7-Jul-06 2:54
Hamid_RT7-Jul-06 2:54 
GeneralRe: Multicolumn listbox /*modified*/ Pin
David Crow7-Jul-06 2:56
David Crow7-Jul-06 2:56 
QuestionParallel Port acess & Reading Char strings to Labels / Lists [modified] Pin
markwalker847-Jul-06 1:21
markwalker847-Jul-06 1:21 
Answer[OT] Re: Parallel Port acess & Reading Char strings to Labels / Lists Pin
David Crow7-Jul-06 2:59
David Crow7-Jul-06 2:59 
GeneralRe: [OT] Re: Parallel Port acess & Reading Char strings to Labels / Lists Pin
markwalker847-Jul-06 3:08
markwalker847-Jul-06 3:08 
AnswerRe: Parallel Port acess & Reading Char strings to Labels / Lists Pin
earl7-Jul-06 6:20
earl7-Jul-06 6:20 
GeneralRe: Parallel Port acess & Reading Char strings to Labels / Lists Pin
markwalker849-Jul-06 22:48
markwalker849-Jul-06 22:48 
GeneralRe: Parallel Port acess & Reading Char strings to Labels / Lists Pin
earl10-Jul-06 9:26
earl10-Jul-06 9:26 
Questionwallpaper on dialog box Pin
ashish dogra7-Jul-06 1:18
ashish dogra7-Jul-06 1:18 
AnswerRe: wallpaper on dialog box /*modified*/ Pin
Hamid_RT7-Jul-06 1:31
Hamid_RT7-Jul-06 1:31 
GeneralRe: wallpaper on dialog box /*modified*/ Pin
ashish dogra7-Jul-06 1:42
ashish dogra7-Jul-06 1:42 
GeneralRe: wallpaper on dialog box Pin
Hamid_RT7-Jul-06 1:47
Hamid_RT7-Jul-06 1:47 
GeneralRe: wallpaper on dialog box [modified] Pin
ashish dogra7-Jul-06 1:54
ashish dogra7-Jul-06 1:54 
GeneralRe: wallpaper on dialog box Pin
Hamid_RT7-Jul-06 3:02
Hamid_RT7-Jul-06 3:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.