stop and re-start timer inside TimerFcn cause pseudo-multiple parallel duplicated timer (in fact only 1 timer exist) running.

2 visualizzazioni (ultimi 30 giorni)
I start a timer and need to update the Period property during the running when some condition was met. inside
(I put the code of updating Period into timerFcn because I want to make the Matlab main thread idle for other ueage.)
Here I use rand>0.9 as the condition (to simulate the GUI switch button is click to downward).
What I found is that, after the condition was met, the timer Period=30 was updated and re-started.
But the old timer with Period=5 was still ongoing as if like a duplicated co-existing timer.
Thus there are 2 timer reporting
disp('timer was called at',datestr(now,31)])
The worse is that if the condition was met again, there will be a third timer running, with the previous two timers still running.
then 4th, 5th,.....the cmd output is overwhelmed with timer reporting.
What's more weird is that timerfindall only shows one timer from beginning to the end.
Is there something wrong with my code?
I am using R2016a.
Thank you.
aTimer = timer(...
'ExecutionMode','fixedSpacing',...
'ObjectVisibility','on',...
'Period',5,...
'StartDelay',1,...
'TimerFcn',@(Obj,~) RMD_flyRelayControl_timer(Obj),...
);
start(aTimer)
function RMD_flyRelayControl_timer(Obj)
if rand>0.9 % or a GUI switch button is click to downward
pause(10);
stop(Obj);
disp('lets update the timer period property')
Obj.Period = 30;pause(1);
start(Obj);pause(1);
return
end
disp('timer was called at',datestr(now,31)])
end
  2 Commenti
raym
raym il 18 Feb 2023
This solves the problem:
delete the timer and re-create a timer inside callback:
if rand>0.9 % or a GUI switch button is click to downward
delete(Obj);
aTimer = timer(...
'ExecutionMode','fixedSpacing',...
'ObjectVisibility','on',...
'Period',30,...
'StartDelay',3,...
'TimerFcn',@(Obj,~)RMD_flyRelayControl_timer(Obj),...
);
start(aTimer);
end

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by