Reputation: 67739
It seems MATLAB handles will not respond to more than one notification from the same source at a time. Unfortunately this is a big problem for me. Here's an example of what I'm talking about:
classdef testClass < handle
events
testevent
end
methods
function obj = testClass
obj.addlistener('testevent', @obj.respond);
end
function raise(obj)
obj.notify('testevent');
end
function respond(obj, varargin)
fprintf('Responded!\n');
obj.raise();
end
end
end
when I execute the code
c = testClass;
c.raise();
The result is
Responded!
But I really want the recursion to work, though obviously in this simple example it would recurse infinitely. Is there any way to get this behavior?
Upvotes: 2
Views: 511
Reputation: 5823
You can make the listener recursive by setting the listener handle's Recursive
property to true
. Listener handle properties are in the event.listener help page. You just need to specify an output argument to obj.addlistener
to retrieve the handle.
Here's the code I used to make it work:
classdef testClass < handle
events
testevent
end
methods
function obj = testClass
lh = obj.addlistener('testevent', @obj.respond);
lh.Recursive = true;
end
function raise(obj)
notify(obj,'testevent');
end
function respond(obj, varargin)
fprintf('Responded!\n');
obj.raise();
end
end
end
Also note that there is a default recursion limit of 500 calls, so naively the code won't infinitely recur; use set(0, 'RecursionLimit', N)
to change that. I would think setting N=inf
is a bad idea.
Upvotes: 4