Why cannot handle class events run indefinitely?
Suppose there are two objects A and B instantiated from some handle classes.
There is an event in A that B listens to, and an event in B that A listens to.
When the event in A is triggered, B trigger its own event, which in turn triggers the event in A, as demonstrated in the figure.
Theoretically, once the event in A is triggered the program should run indefinitely. However in practice the program terminates when the event in A has been triggered twice. Why?
Take following code snippets for example.
The class definition
classdef myClass < handle
properties
Name
end
events
theEvent
end
methods
function obj = myClass(c)
obj.Name = c;
end
function showName(obj)
disp([‘This is obj ‘, obj.Name])
notify(obj, ‘theEvent’)
end
end
end
The main file to run
A = myClass(‘A’);
B = myClass(‘B’);
addlistener(A, ‘theEvent’, @(varargin)B.showName());
addlistener(B, ‘theEvent’, @(varargin)A.showName());
disp(‘Program started’)
showName(A)
disp(‘Program terminated’)Suppose there are two objects A and B instantiated from some handle classes.
There is an event in A that B listens to, and an event in B that A listens to.
When the event in A is triggered, B trigger its own event, which in turn triggers the event in A, as demonstrated in the figure.
Theoretically, once the event in A is triggered the program should run indefinitely. However in practice the program terminates when the event in A has been triggered twice. Why?
Take following code snippets for example.
The class definition
classdef myClass < handle
properties
Name
end
events
theEvent
end
methods
function obj = myClass(c)
obj.Name = c;
end
function showName(obj)
disp([‘This is obj ‘, obj.Name])
notify(obj, ‘theEvent’)
end
end
end
The main file to run
A = myClass(‘A’);
B = myClass(‘B’);
addlistener(A, ‘theEvent’, @(varargin)B.showName());
addlistener(B, ‘theEvent’, @(varargin)A.showName());
disp(‘Program started’)
showName(A)
disp(‘Program terminated’) Suppose there are two objects A and B instantiated from some handle classes.
There is an event in A that B listens to, and an event in B that A listens to.
When the event in A is triggered, B trigger its own event, which in turn triggers the event in A, as demonstrated in the figure.
Theoretically, once the event in A is triggered the program should run indefinitely. However in practice the program terminates when the event in A has been triggered twice. Why?
Take following code snippets for example.
The class definition
classdef myClass < handle
properties
Name
end
events
theEvent
end
methods
function obj = myClass(c)
obj.Name = c;
end
function showName(obj)
disp([‘This is obj ‘, obj.Name])
notify(obj, ‘theEvent’)
end
end
end
The main file to run
A = myClass(‘A’);
B = myClass(‘B’);
addlistener(A, ‘theEvent’, @(varargin)B.showName());
addlistener(B, ‘theEvent’, @(varargin)A.showName());
disp(‘Program started’)
showName(A)
disp(‘Program terminated’) handle class events, infinite loop, oop MATLAB Answers — New Questions