フィルターのクリア

Listening an event when another function of the class is running?

9 ビュー (過去 30 日間)
Michaël
Michaël 2016 年 3 月 16 日
コメント済み: Michaël E 2017 年 6 月 8 日
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

採用された回答

Guillaume
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 件のコメント
Michaël
Michaël 2016 年 3 月 17 日
Thank you very much!
Which "ideal" language are you thinking of for this kind of programming?
Guillaume
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
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
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?
Michaël
Michaël 2016 年 3 月 17 日
Yes, it's in the while loop.

サインインしてコメントする。


ARPIT
ARPIT 2017 年 6 月 7 日
Hey Michael,
Can you kindly suggest how you finally implemented it because I also want to do exactly similar thing.
Thanks in advance
  1 件のコメント
Michaël E
Michaël E 2017 年 6 月 8 日
Hi,
Matlab isn't able to run concomitantly two (while) loops; a workaround is to use the "timer" object.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by