System::Threading::Timer class

Timer class

Timer class that executes job item in separate thread after delay. Objects of this class should only be allocated using System::MakeObject() function. Never create instance of this type on stack or using operator new, as it will result in runtime errors and/or assertion faults. Always wrap this class into System::SmartPtr pointer and use this pointer to pass it to functions as argument.

class Timer : public System::Object

Methods

MethodDescription
Change(int64_t, int64_t)Re-schedules or cancels timer.
Change(System::TimeSpan, System::TimeSpan)Re-schedules or cancels timer.
Dispose()De-schedules timer.
Timer(TimerCallback)Constructor.
Timer(TimerCallback, const System::SharedPtr<System::Object>&, int64_t, int64_t)Constructor.
Timer(TimerCallback, const System::SharedPtr<System::Object>&, System::TimeSpan, System::TimeSpan)Constructor.

Remarks

#include "system/threading/thread.h"
#include "system/threading/timer.h"
#include "system/object.h"
#include "system/smart_ptr.h"
#include <iostream>

int main()
{
  using namespace System::Threading;

  auto number = 0;
  auto timer = System::MakeObject<Timer>([&number](System::SharedPtr<System::Object> object) -> void {
    std::cout << ++number << std::endl;
  }, nullptr, 0, 200);

  Thread::Sleep(1000);
  timer->Dispose();

  return 0;
}
/*
This code example produces the following output:
1
2
3
4
5
*/

See Also