Search code examples
c++visual-studiowxwidgetssleep

wxWidgets timer doesn't start?


This wxWidgets code (simulating a portable sleep), does not work.

MyClass::MyClass(){
m_timer = std::make_shared<wxTimer>(this, ID_TIMER);
}

void MyClass::WaitOneSecond()
{
  m_timer->StartOnce(1000);
  while (m_timer->GetInterval() < 1000);
}

When stepping with Visual Studio over a WaitOneSecond call, no delay appears. Why ?...


Solution

  • A wxwTimer works with the WxWidget's event loop.

    That means it will notify you after the delay as expired, but you must give control back.

    As explained in the documentation:

    There are three different ways to use this class:

    • You may derive a new class from wxTimer and override the wxTimer::Notify member to perform the required action.
    • You may redirect the notifications to any wxEvtHandler derived object by using the non-default constructor or wxTimer::SetOwner. Then use the EVT_TIMER macro to connect it to the event handler which will receive wxTimerEvent notifications.
    • You may use a derived class and the EVT_TIMER macro to connect it to an event handler defined in the derived class. If the default constructor is used, the timer object will be its own owner object, since it is derived from wxEvtHandler.