wxWidgets 计时器不启动?

wxWidgets timer doesn't start?

此 wxWidgets 代码(模拟便携式 sleep)不起作用。

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

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

当使用 Visual Studio 单步执行 WaitOneSecond 调用时,不会出现延迟。 为什么?...

A​​ wxwTimerWxWidget 的事件循环一起工作。

这意味着它会在延迟到期后通知您,但您必须交还控制权。

如文档中所述:

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.

wxWidgets有休眠功能 wxMilliSleep

C++11 有一个每线程休眠:std::this_thread::sleep_for