Listening an event when another function of the class is running?
조회 수: 9 (최근 30일)
이전 댓글 표시
Hello,
My class looks like:
classdef linescan99 < handle
methods
function obj = linescan99(obj_surveilleur_de_contact)
addlistener(obj_surveilleur_de_contact,'Contact',@obj.handleEvnt);
end
function follower(obj)
while 1
%computations
end
end
end
end
I've noted that when the follower function is executing, the class is unable to listen a possible event 'Contact'. What's unfortunate: this event would lead to a break of the follower function while 1 loop...
Do you know how to force the function to listen the event? maybe inside the loop?
Thank you very much
댓글 수: 0
채택된 답변
Guillaume
2016년 3월 17일
I think the problem is that your function suiveur is called by the event handler, so your while loop is executing while an event is already being handled. I'll be very surprised if event handlers are reentrant in matlab, so no new event can be handled until you've finished dealing with the current one (in effect until you've quit your loop).
You're trying to do some multithreaded (or asynchronous) programming here which is not really supported by matlab. A possible workaround would be to use a timer to periodically check whether or not the event has been raised:
%Warning: completely untested code. There may be typos:
classdef linescan99 < handle
properties (SetAccess = private)
suiveur_en_cour = false;
minuteur;
tracking = false;
end
methods
function this = linescan99(obj_surveilleur_de_contact) %constructor
addlistener(obj_surveilleur_de_contact,'Contact',@obj.handleEvnt);
this.minuteur = timer('BusyMode', 'drop', 'ExecutionMode', 'fixedRate', 'Period', 0.1, 'TimerFcn', @obj.suiveur);
this.minuteur.start();
end
function handleEvnt(this, obj_surveilleur_de_contact,~)
if obj_surveilleur_de_contact.State %contact
disp('linescan lancé')
end
this.suiveur_en_cour = obj_surveilleur_de_contact.State;
end
function suiveur(this, ~, ~)
if suiveur_en_cour
if ~tracking %this is just to make sure disp is only called once, when tracking begins
tracking = true;
disp('tracking en cours');
end
%do whatever you wanted to do in your while loop
else
tracking = false;
end
end
end
end
If this works for you, you could also simply store a reference to obj_surveilleur_de_contact in linescan99, check its state with the timer and not bother with the listener or events.
댓글 수: 2
Guillaume
2016년 3월 18일
With the design you have, what you want is to launch a background or asynchronous task from the event handler, so that the handler can return immediately.
Matlab parallel processing toolbox would probably let you do that easily but since I don't have and have never used that toolbox, I can't help you with that.
The 'ideal' language is the one you know, really. Among the ones I know, asynchronous programming is very easy with .Net (so C#, VB.Net or C++/.net) .
추가 답변 (2개)
Geoff Hayes
2016년 3월 17일
Michaël - without seeing more of your code, in particular the handleEvent or all of follower, it is difficult to know for sure what is going wrong. But if I were to guess, it may be because of your while loop
while 1
% computations
end
Is this a "tight" while loop that does not allow itself to be interrupted? Are there any calls to drawnow or pause? If not, then this could mean that the while loop will continue to run and monopolize all the processing thus not "giving a chance" to any other call until it has been completed. If I ever write something similar, then I put a short pause in the code to temporarily halt execution and allow other events (callbacks etc.) to occur.
while 1
% computations
% pause for fraction of a second
pause(0.01);
end
Try the above and see what happens!
댓글 수: 5
Geoff Hayes
2016년 3월 17일
...the while 1 of my first class doesn't seem to be executed during this pause
Isn't the pause in the while loop? Can you post the code (i.e. attach) so that we can see the changes?
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!