-3

I need an example of a timer to use. At the end, it should be an object to whom I would pass the duration (X ms) and a pointer to the function that should be called when the timer elapses (after X ms).

If anybody has an implementation, it would be great, but also advices would be very useful. I have never implemented any timer in C++.

Thank you in advance!

Dusan
  • 95
  • 5
  • Hint google "C++ sleep" and work your object around that. but you need to show a little effort before expecting code from the community. – Rob Nov 30 '16 at 15:37
  • 1
    Since there are several C++ variants , this is a possible duplicate of http://stackoverflow.com/questions/14650885/how-to-create-timer-events-using-c-11 – infixed Nov 30 '16 at 15:53
  • I will study the chrono and implement my own, but I am out of time these days and one working example would be great. But I found one and it works. – Dusan Dec 02 '16 at 23:00
  • Are you using boost? POSIX threads? The Windows API? Is your code single threaded? If not, what threading library are you using? If so, how do you want the timer to fire exactly? – David Schwartz Dec 16 '16 at 02:58

2 Answers2

0

// This starts a managed obj of a timer immediately, and with a 1 Second tick,
System::Threading::Timer myTimer = gcnew System::Threading::Timer(gcnew System::Threading::TimerCallback(Htask), NULL, 0, 1000);

// This is the function call back syntax
void Htask(Object ^state)
{
    static int counter = 0;
    printf("Executing my timer %d", counter);
    return;
}
Dominick
  • 31
  • 7
0

I forgot to add, to use that you need to enable CLR, if you use VS 2015 is under "Project" >> your project property, Then go under "Configuration Property" and "General" and change the option "Common Language Runtime Support" to "Common Language Support (/clr)".

Then use #import <System.dll>

uning namespace System::Threading;

will save you from keep typing System::......

Regards Dominick

Dominick
  • 31
  • 7