|
-
March 20th, 2013, 07:50 AM
#3
Re: timers with 1ms of precision
 Originally Posted by VictorN
i found these function
CreateTimerQueueTimer() maybe i find 1 sample. but, please see my class:
Code:
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <Mmsystem.h>
class Timer
{
private:
int MilliSecondsTimer;
private:
bool blnDestroyed;
private:
HANDLE Handle_Of_Thread_1;
typedef void (*MyTimerProc)();
MyTimerProc TimerProcedure;
static DWORD WINAPI StaticThreadProc(LPVOID param) {
static_cast<Timer*>(param)->MultithreadProcedure();
return 0;
}
public:
int SetMilliSeconds(int MilliSeconds)
{
MilliSecondsTimer = MilliSeconds;
return 0;
}
public:
int GetMilliSeconds()
{
return (MilliSecondsTimer);
}
private: void MultithreadProcedure()
{
/*LARGE_INTEGER liFrequency;
LARGE_INTEGER liStartTime;
LARGE_INTEGER liCurrentTime;
double a;
QueryPerformanceFrequency ( &liFrequency );
QueryPerformanceCounter ( &liStartTime );
for (;;)
{
QueryPerformanceCounter ( &liCurrentTime );
a=(int) ((double)( (liCurrentTime.QuadPart - liStartTime.QuadPart)* (double)1000.0/(double)liFrequency.QuadPart ));
if (a>=MilliSecondsTimer)
{
TimerProcedure();
QueryPerformanceCounter ( &liStartTime );
}
if (blnDestroyed==true) break;
}*/
int a=0;
for (;;)
{
if (blnDestroyed==true) break;
timeBeginPeriod(1) ;
Sleep(1) ;
timeEndPeriod(1);
a=a+1;
if (a ==MilliSecondsTimer)
{
TimerProcedure();
a=0;
}
}
CloseHandle(Handle_Of_Thread_1);
TerminateThread( Handle_Of_Thread_1,1);
}
public:
int Start(MyTimerProc TimerProc)
{
TimerProcedure = TimerProc;
Handle_Of_Thread_1=0;
CloseHandle(Handle_Of_Thread_1);
TerminateThread( Handle_Of_Thread_1,1);
blnDestroyed=false;
HANDLE hMyThread;
hMyThread = CreateThread(NULL, 0, StaticThreadProc, this, 0, NULL);
return 0;
}
public: int Destroy()
{
blnDestroyed=true;
return 0;
}
};
Timer test;
int a;
void TimerProcedure()
{
system("cls");
a=a+1;
printf("%d\n",a);
}
void main()
{
test.SetMilliSeconds(10);
test.Start(TimerProcedure);
getch();
test.Destroy();
}
why i don't get 100ms( or 10ms, at least)?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|