Computing.Net > Forums > Programming > C++ timer problems

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

C++ timer problems

Reply to Message Icon

Name: elhan
Date: November 27, 2003 at 02:23:12 Pacific
OS: Win XP
CPU/Ram: Unknown
Comment:

Hi folks.

I am trying to get a c++ timer to work in the sort of resolution of around 10 ms.

AT the moment I am using a test loop with
int time = 40;
for(i=0; i < 30; i++)
{
a = a+1;
Sleep(time);
}

The basic theory was to sample a every '40' ms.

However I am trying with the following code to give a smilar thing.

#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include <iostream.h>

void main (void)
{
TIMECAPS resolution;
DWORD start, finish, duration, i;

if (timeGetDevCaps (&resolution, sizeof (TIMECAPS)) == TIMERR_NOERROR)
{
printf ("Minimum supported resolution = %d\n", resolution.wPeriodMin);
printf ("Maximum supported resolution = %d\n", resolution.wPeriodMax);
}

if (resolution.wPeriodMin <= 1)
{
if (timeBeginPeriod (1) == TIMERR_NOERROR)
{
for (i = 1; i <= 20; i++)
{
start = timeGetTime ();
while (timeGetTime () < (start + i));

finish = timeGetTime ();
printf ("start:%d finish:%ld\n", start, finish);
duration = finish - start;
printf ("expected:%d actual:%ld\n", i, duration);
}
timeEndPeriod (1);
}
}

}

except I get a lot of undeclared errors for some reason.

I cannot figure out what is causing this.

I would appreciate any help.

Thanks

Elhan



Sponsored Link
Ads by Google

Response Number 1
Name: myxp
Date: November 27, 2003 at 05:05:05 Pacific
Reply:

Elhan:

You're mixing up the Windows API interface
with the C++ console application interface.


MYXP


0

Response Number 2
Name: jake1025
Date: November 27, 2003 at 18:33:49 Pacific
Reply:

use this as a timer (20000 = about 1 second depending on your processor speed):

int main(){
//stuff

int time = 10000;
while (time != 0){
cout <<"_\r";
//you must have something for the computer to
//to take up time wasted otherwise there will
//no no noticable delay. This outputs an _
//nad erases it immeaditely with \r.
time--;
}

//other stuff
}

hope this works!
Jake


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Winsock, Firewall & P... avi to vcd



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ timer problems

C++ Timer www.computing.net/answers/programming/c-timer/16630.html

simple C++ compile problem in Linux www.computing.net/answers/programming/simple-c-compile-problem-in-linux-/1282.html

C++ Homework problem www.computing.net/answers/programming/c-homework-problem/8556.html