parallel timers not executing as expected
I am trying to run two parallel timers. Below is a minimal example.
I would expect (or wish) that timerCallback2 is executed when timerCallback1 is in pause mode. But the function seems to be blocking. I also tried to add some drawnow calls.
Is there any way to execute multiple timer callbacks while another is still running, or is this intended behavior? Does MATLAB not process the event queue while another event is still running? Or are there different event queues for timer events and GUI events?
Thank you for your help!
timer1 = timer(Name="timer1", ExecutionMode="fixedSpacing", BusyMode="queue", TimerFcn=@timerCallback1, TasksToExecute=3, Period=1);
start(timer1)
timer2 = timer(Name="timer2", ExecutionMode="fixedSpacing", BusyMode="queue", TimerFcn=@timerCallback2, TasksToExecute=30, Period=0.1);
start(timer2)
function timerCallback1(~,~)
disp("start T1…")
pause(2)
disp("end T1…")
end
function timerCallback2(~, ~)
disp(" start T2…")
pause(0.05)
disp(" end T2…")
end
%% Output is:
% start T1…
% end T1…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T1…
% end T1…
% start T2…
% end T2…
% start T2…
% and so on….I am trying to run two parallel timers. Below is a minimal example.
I would expect (or wish) that timerCallback2 is executed when timerCallback1 is in pause mode. But the function seems to be blocking. I also tried to add some drawnow calls.
Is there any way to execute multiple timer callbacks while another is still running, or is this intended behavior? Does MATLAB not process the event queue while another event is still running? Or are there different event queues for timer events and GUI events?
Thank you for your help!
timer1 = timer(Name="timer1", ExecutionMode="fixedSpacing", BusyMode="queue", TimerFcn=@timerCallback1, TasksToExecute=3, Period=1);
start(timer1)
timer2 = timer(Name="timer2", ExecutionMode="fixedSpacing", BusyMode="queue", TimerFcn=@timerCallback2, TasksToExecute=30, Period=0.1);
start(timer2)
function timerCallback1(~,~)
disp("start T1…")
pause(2)
disp("end T1…")
end
function timerCallback2(~, ~)
disp(" start T2…")
pause(0.05)
disp(" end T2…")
end
%% Output is:
% start T1…
% end T1…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T1…
% end T1…
% start T2…
% end T2…
% start T2…
% and so on…. I am trying to run two parallel timers. Below is a minimal example.
I would expect (or wish) that timerCallback2 is executed when timerCallback1 is in pause mode. But the function seems to be blocking. I also tried to add some drawnow calls.
Is there any way to execute multiple timer callbacks while another is still running, or is this intended behavior? Does MATLAB not process the event queue while another event is still running? Or are there different event queues for timer events and GUI events?
Thank you for your help!
timer1 = timer(Name="timer1", ExecutionMode="fixedSpacing", BusyMode="queue", TimerFcn=@timerCallback1, TasksToExecute=3, Period=1);
start(timer1)
timer2 = timer(Name="timer2", ExecutionMode="fixedSpacing", BusyMode="queue", TimerFcn=@timerCallback2, TasksToExecute=30, Period=0.1);
start(timer2)
function timerCallback1(~,~)
disp("start T1…")
pause(2)
disp("end T1…")
end
function timerCallback2(~, ~)
disp(" start T2…")
pause(0.05)
disp(" end T2…")
end
%% Output is:
% start T1…
% end T1…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T2…
% end T2…
% start T1…
% end T1…
% start T2…
% end T2…
% start T2…
% and so on…. timer, event, callback MATLAB Answers — New Questions