Help in A timer in C++

I was making a Quiz in the Console.

I decided to add an Timer in that runs in the background, and gives the user the Time they took at the end.

This is the format of my Timer:
1
2
3
4
5
6
7
8
void Timer (void*)
{
    while(TimerOn)    //TimerOn is a Global Variable of Type bool.
    {
        TotalTime += 1;  //Another global variable of type int.
        Sleep(1);
    }
}


I added void* in the parameter list to make it compatible with the pointer with the pointer in the _beginthread function from <process.h>.

Now What I wanted to know is whether this is an effective method to get the time in Milliseconds?


windows ?
In windows you can use GetTickCount
it is a background timer and it tells time in milliseconds. (From windows start. )
Last edited on
I guess that GetTickCount will tell the time the program has been running from the since it started?
no. It will tell the time since WINDOWS HAS BEEN RUNNING. But you can save it first then subtract gettickcount from saved time.
See here for an example of how to time stuff. You don't need a separate thread.
http://www.cplusplus.com/forum/beginner/28855/3/#msg159698
Hope this helps.
@Douas:
Had a quick look at you code. I will try it out...
But what is the Header: <ciso646> for?
operator synonyms. i tend to use them, but I don't think the timer code uses any. [so you can probably do without including it]
Last edited on
What are operator Synonyms?
Googling gives me Synonyms for Operator!!!

EDIT:
There is a problem here though.
The same one I had when using ctime.
The problem is that the times starts the moment you run the program.
That was why I decided to use the above method. Any workaround for that?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "windows.h"

DWORD startTime;
 
int main()
{
   startTime = GetTickCount();

   DWORD elapsedTime;
   while(true)
   {
      elapsedTime = GetTickCount() - startTime;
      if( elapsedTime >= QUIZ_TIME_IN_MILLISECONDS ) break;
   }
}
Topic archived. No new replies allowed.