At busy times, in multiple-execution scenarios, the timer may
need to add the timer callback function (TimerFcn
)
to the MATLAB® execution queue before the previously queued execution
of the callback function has completed. You can determine how the
timer object handles this scenario by setting the BusyMode
property
to use one of these modes:
If you specify 'drop'
as the value of the BusyMode
property,
the timer object adds the timer callback function to the execution
queue only when the queue is empty. If the execution queue is not
empty, the timer object skips the execution of the callback.
For example, suppose you create a timer with a period of 1 second,
but a callback that requires at least 1.6 seconds, as shown here for mytimer.m
.
function mytimer() t = timer; t.Period = 1; t.ExecutionMode = 'fixedRate'; t.TimerFcn = @mytimer_cb; t.BusyMode = 'drop'; t.TasksToExecute = 5; t.UserData = tic; start(t) end function mytimer_cb(h,~) timeStart = toc(h.UserData) pause(1.6); timeEnd = toc(h.UserData) end
This table describes how the timer manages the execution queue.
Approximate Elapsed Time (Seconds) | Action |
---|---|
0 | Start the first execution of the callback. |
1 | Attempt to start the second execution of the callback. The first execution is not complete, but the execution queue is empty. The timer adds the callback to the queue. |
1.6 | Finish the first callback execution, and start the second. This action clears the execution queue. |
2 | Attempt to start the third callback execution. The second execution is not complete, but the queue is empty. The timer adds the callback to the queue. |
3 | Attempt to start the fourth callback execution. The third callback is in the execution queue, so the timer drops this execution of the function. |
3.2 | Finish the second callback and start the third, clearing the execution queue. |
4 | Attempt to start another callback execution. Because the queue is empty, the timer adds the callback to the queue. This is the fifth attempt, but only the fourth instance that will run. |
4.8 | Finish the third execution and start the fourth instance, clearing the queue. |
5 | Attempt to start another callback. An instance is running, but the execution queue is empty, so the timer adds it to the queue. This is the fifth instance that will run. |
6 | Do nothing: the value of the |
6.4 | Finish the fourth callback execution and start the fifth. |
8 | Finish the fifth callback execution. |
The 'error'
mode for the BusyMode
property
is similar to the 'drop'
mode: In both modes, the
timer allows only one instance of the callback in the execution queue.
However, in 'error'
mode, when the queue is nonempty,
the timer calls the function that you specify using the ErrorFcn
property,
and then stops processing. The currently running callback function
completes, but the callback in the queue does not execute.
For example, modify mytimer.m
(described
in the previous section) so that it includes an error handling function
and sets BusyMode
to 'error'
.
function mytimer() t = timer; t.Period = 1; t.ExecutionMode = 'fixedRate'; t.TimerFcn = @mytimer_cb; t.ErrorFcn = @myerror; t.BusyMode = 'error'; t.TasksToExecute = 5; t.UserData = tic; start(t) end function mytimer_cb(h,~) timeStart = toc(h.UserData) pause(1.6); timeEnd = toc(h.UserData) end function myerror(h,~) disp('Reached the error function') end
This table describes how the timer manages the execution queue.
Approximate Elapsed Time (Seconds) | Action |
---|---|
0 | Start the first execution of the callback. |
1 | Attempt to start the second execution of the callback. The first execution is not complete, but the execution queue is empty. The timer adds the callback to the queue. |
1.6 | Finish the first callback execution, and start the second. This action clears the execution queue. |
2 | Attempt to start the third callback execution. The second execution is not complete, but the queue is empty. The timer adds the callback to the queue. |
3 | Attempt to start the fourth callback execution. The third callback is in the execution queue. The timer does not execute the third callback, but instead calls the error handling function. |
3.2 | Finish the second callback and start the error handling function. |
If you specify 'queue'
, the timer object
waits until the currently executing callback function finishes before
queuing the next execution of the timer callback function.
In 'queue'
mode, the timer object tries to
make the average time between executions equal the amount of time
specified in the Period
property. If the timer
object has to wait longer than the time specified in the Period
property
between executions of the timer function callback, it shortens the
time period for subsequent executions to make up the time.