CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Timer question

  1. #1
    Join Date
    Mar 2005
    Posts
    32

    Timer question

    I need to find out the time elapsed between 2 events. Can someone help me with it. An example would be great.
    Thanks in advance.

  2. #2
    Join Date
    Sep 1999
    Posts
    137

    Re: Timer question

    I have attached a class that wraps some API calls to make dealing with time a little easier.

    To use it

    Code:
    #include "Timeout.h"
    
    class MyClass
    {
        CTimeout m_to;
    
        void OnEvent();
    }
    
    void CMyClass::OnEvent()
    {
        if (m_to.IsStarted())
            m_to.Start();
        else
            printf( "%d", m_to.TimeElapsed() );
    }
    Attached Files Attached Files

  3. #3
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    Re: Timer question

    Hello,

    What will be the order of time? One can use CTime::GetCurrentTime function to get the time as CTime and use CTime::GetAsSystemTime function to convert the time to a value of type SYSTEMTIME. wMilliseconds member of SYSTEMTIME structure can be used to get the time in milliseconds.

    One can use the function GetTickCount to get the milliseconds elapsed since the system is started.

    If you want to clock the events more accurately, you can use the function QueryPerformanceCounter in conjuction with the function QueryPerformanceFrequency.

    Regards. Please rate this post if this suggestion is helpful.

    Pravin.
    04-05-2005.

  4. #4
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    Re: Timer question

    Hello,

    Here is a function which returns the time difference in seconds between its call and the previous call.

    Code:
    
    float DTime()
    {
    	static LARGE_INTEGER OldCounter = {0, 0};
    	LARGE_INTEGER Counter, Frequency;
    	if (QueryPerformanceFrequency(&Frequency))
    	{
    		QueryPerformanceCounter(&Counter);
    		float TimeDiff = OldCounter.LowPart ? (float)
    			(Counter.LowPart - OldCounter.LowPart) / Frequency.LowPart : 0;
    		OldCounter = Counter;
    		return TimeDiff;
    	}
    	else
    		return 0;
    }
    
    If it is called the first time, return value is 0. If there is no high-resolution performance counter, it returns 0 each time.

    Does it help?

    Pravin.
    04-05-2005.

  5. #5
    Join Date
    Mar 2005
    Posts
    32

    Re: Timer question

    Thank you guys very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured