Class: RUtilAnts::GUI::SafeTimersManager
- Inherits:
-
Object
- Object
- RUtilAnts::GUI::SafeTimersManager
- Defined in:
- lib/rUtilAnts/GUI.rb
Overview
Manager that handles normal Wx::Timer, integrating a mechanism that can kill it and wait until it has been safely killed. Very handy for timers processing data that might be destroyed. To be used with safeTimerAfter and safeTimerEvery.
Instance Method Summary collapse
-
#initialize ⇒ SafeTimersManager
constructor
Constructor.
-
#killTimers ⇒ Object
Kill all registered Timers and wait for their completion.
-
#registerTimer(iTimer) ⇒ Object
Register a given timer.
-
#unregisterTimer(iTimer) ⇒ Object
Unregister a given timer.
Constructor Details
#initialize ⇒ SafeTimersManager
Constructor
312 313 314 315 316 |
# File 'lib/rUtilAnts/GUI.rb', line 312 def initialize # List of registered timers # list< Wx::Timer > @Timers = [] end |
Instance Method Details
#killTimers ⇒ Object
Kill all registered Timers and wait for their completion. Does not return unless they are stopped.
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/rUtilAnts/GUI.rb', line 349 def killTimers # Notify each Timer that it has to stop @Timers.each do |ioTimer| ioTimer.stop end # Wait for each one to be stopped lTimersToStop = [] # Try first time, to not enter the loop if they were already stopped @Timers.each do |iTimer| if (iTimer.is_running) lTimersToStop << iTimer end end while (!lTimersToStop.empty?) lTimersToStop.delete_if do |iTimer| next (!iTimer.is_running) end # Give time to the application to effectively stop its timers Wx.get_app.yield # Little sleep sleep(0.1) end end |
#registerTimer(iTimer) ⇒ Object
Register a given timer
- Parameters
-
iTimer (Wx::Timer): The timer to register
322 323 324 |
# File 'lib/rUtilAnts/GUI.rb', line 322 def registerTimer(iTimer) @Timers << iTimer end |
#unregisterTimer(iTimer) ⇒ Object
Unregister a given timer
- Parameters
-
iTimer (Wx::Timer): The timer to unregister
- Return
-
Boolean: Was the Timer registered ?
332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/rUtilAnts/GUI.rb', line 332 def unregisterTimer(iTimer) rFound = false @Timers.delete_if do |iRegisteredTimer| if (iRegisteredTimer == iTimer) rFound = true next true else next false end end return rFound end |