indexing description: "Execute an action every interval until stopped." author: "Patrick Ruckstuhl " date: "$Date$" revision: "$Revision$" class TIMER inherit THREAD export {NONE} all end create make feature {NONE} -- Initialization make (a_action: like action; a_interval: like interval) is -- Create a timer that executes a_action every a_interval milliseconds. require a_action_set: a_action /= Void a_interval_valid: a_interval > 0 do action := a_action interval := a_interval create timer_mutex.make create timer_condition.make is_stop := True ensure action_set: action = a_action interval_set: interval = a_interval end feature -- Status is_stop: BOOLEAN -- Is the timer stopped? feature -- Access action: PROCEDURE [ANY, TUPLE[]] -- Action to execute every interval. interval: INTEGER -- Milliseconds to wait until action is called again. feature -- Commands start is -- Start the timer. do is_stop := False launch end stop is -- Stop the timer. do is_stop := True timer_mutex.lock timer_condition.signal timer_mutex.unlock join end feature {NONE} -- Implementation timer_mutex: MUTEX -- Mutex for timer. timer_condition: CONDITION_VARIABLE -- Condition variable to wait on during the interval. execute is -- Main loop, executes action every interval until stopped. local l_tmp: BOOLEAN do timer_mutex.lock from until is_stop loop action.call ([]) l_tmp := timer_condition.wait_with_timeout (timer_mutex, interval) end timer_mutex.unlock end invariant action_set: action /= Void interval_valid: interval > 0 timer_mutex_not_void: timer_mutex /= Void timer_condition_not_void: timer_condition /= Void end