Simplify the code by using CallAfter()

Use CallAfter() to perform idle-time initialization, this is shorter and
simpler than dealing with the idle events explicitly.

Unfortunately the remaining use of idle events can't be avoided, explain
the reasons for this in a comment.
This commit is contained in:
Vadim Zeitlin
2019-04-25 02:58:53 +02:00
parent 9c956ea962
commit 4783262238

View File

@@ -211,12 +211,12 @@ public:
, m_loopActivator(&m_loop) , m_loopActivator(&m_loop)
#endif #endif
{ {
Bind(wxEVT_FSWATCHER, &FSWTesterBase::OnFileSystemEvent, this);
// wxFileSystemWatcher can be created only once the event loop is // wxFileSystemWatcher can be created only once the event loop is
// running, so we can't do it from here and will do it from inside the // running, so we can't do it from here and will do it from inside the
// loop when this event handler is invoked. // loop when this event handler is invoked.
Bind(wxEVT_IDLE, &FSWTesterBase::OnIdleInit, this); CallAfter(&FSWTesterBase::OnIdleInit);
Bind(wxEVT_FSWATCHER, &FSWTesterBase::OnFileSystemEvent, this);
} }
virtual ~FSWTesterBase() virtual ~FSWTesterBase()
@@ -246,20 +246,23 @@ public:
void Run() void Run()
{ {
SendIdle();
m_loop.Run(); m_loop.Run();
} }
void OnIdleInit(wxIdleEvent& WXUNUSED(event)) void OnIdleInit()
{ {
// We shouldn't be called again.
Unbind(wxEVT_IDLE, &FSWTesterBase::OnIdleInit, this);
CPPUNIT_ASSERT(Init()); CPPUNIT_ASSERT(Init());
GenerateEvent(); GenerateEvent();
// Check the result when the next idle event comes. // Check the result when the next idle event comes: note that we can't
// use CallAfter() here, unfortunately, because OnIdleCheckResult()
// would then be called immediately, from the same event loop iteration
// as we're called from, because the idle/pending events are processed
// for as long as there any. Instead, we need to return to the event
// loop itself to give it a chance to dispatch wxFileSystemWatcherEvent
// and wait until our handler for it calls SendIdle() which will then
// end up calling OnIdleCheckResult() afterwards.
Bind(wxEVT_IDLE, &FSWTesterBase::OnIdleCheckResult, this); Bind(wxEVT_IDLE, &FSWTesterBase::OnIdleCheckResult, this);
} }