timer or while-loop is the question

조회 수: 20 (최근 30일)
per isakson
per isakson 2013년 3월 8일
It is possible to emulate a simple timer with a while-loop as I outline below.
The timer object causes me trouble and now I think a construct based on a while-loop would serve better in my specific case. I need a timer that runs "forever" and invokes a "callback function" every few seconds. It should be able to recover after run-time errors in the "callback function".
Reasons to use a while-loop:
Reasons to use the timer:
  • "The advantage of the timer is it allows you to do something else while the timer is waiting giving you the appearance of multi-threading." copied from Daniels answer.
  • better control over when callbacks are fired (see Daniels answer)
  • less and more readable code
Have I missed something important?
.
Out-line of simple "timer" based on a while-loop
period = 1;
tasks2execute = 1;
TasksToExecute = 3;
start_delay = 0;
pause( start_delay )
while tasks2execute <= TasksToExecute
try
timer_function( ... )
catch me
disp( 'cleanup' )
end
pause( period )
tasks2execute = tasks2execute + 1;
end
.
and my timer construct; "my specific case" (added 2013-02-09)
tmr = timer('Name' , 'my_timer' ...
, 'TimerFcn' , @timer_function ...
, 'BusyMode' , 'drop' ...
, 'ExecutionMode' , 'fixedDelay' ...
, 'Period' , 1 ...
, 'StartDelay' , 1 ...
, 'TasksToExecute', 999999 ...
);
start( tmr )
wait ( tmr )

채택된 답변

Daniel Shub
Daniel Shub 2013년 3월 9일
편집: Daniel Shub 2013년 3월 9일
I think your reasons to use a timer are wrong. Since timers are running in an alternate thread they don't always fire when they should. A loop can only be interrupted when you say it can. This means you can block out interrupts 100ms before the callback and be pretty much guaranteed the callback fires when it should. As for less code you could write a timerloop function so you would only have 1 line of code.
The advantage of the timer is it allows you to do something else while the timer is waiting giving you the appearance of multi threading. Imagine a GUI stop watch that updated every second. From the commandline you might type start(stopwatch). If you used a timer it would return instantly. With a timer loop it would never return.
I personally find Timer object useless for anything requiring better than 10s accuracy.
  댓글 수: 5
per isakson
per isakson 2013년 3월 9일
Daniel,
I did not honor KISS and have paid for that. However, I learned something.
TMW ought to add your sentence, "The advantage of the timer ...", to the documentation. They typically provide tools without discussing, for which tasks they are appropriate.
The try-catch-wrapper for timer_function, which you proposed, is really helpful when debugging.
Thanks, Per
Daniel Shub
Daniel Shub 2013년 3월 10일
Per, some discussion about when timers can fire, and links to documentation can be found in this old question (although the documentation appears to be wrong). I will follow this up with a new question.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Code Execution에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by