How to find listeners attached to an object

21 ビュー (過去 30 日間)
Daniel
Daniel 2014 年 8 月 14 日
コメント済み: DNF 2021 年 4 月 27 日
Hi,
I thought this would be a trivial task, but I cannot find the way to do this. Given an object in your workspace, is there an easy way to get handles to all the listeners that have been defined on this object?
cheers, Daniel
  5 件のコメント
Adam
Adam 2019 年 11 月 15 日
From a code design perspective the original question does seem to head down a route that is not generally advisable. An object that allows listeners to be attached should not ever need to know what those listeners are. The example Adam Danz gave is understandable, to know if there are listeners at all, but an object knowing actually what is listening to it seems like a case of knowing far too much.
It would be akin to a radio host being able to find out the names of everyone listening to his show!
That's not to say I haven't also fallen into numerous traps with listeners firing and giving me pages of errors for objects that no longer exist, but whose listener still exists, etc, etc, but it should never be up to the object being listened to to directly interact with those doing the listening. Otherwise some more concrete form of two-way object interaction should be used I would think, as listeners are inherently a one-way communication.
Adam Danz
Adam Danz 2019 年 11 月 15 日
編集済み: Adam Danz 2019 年 11 月 15 日
I wish there were a way to h=findall('listeners') or h=findall(obj,'listeners') or something like that because some listeners are created by functions that do not return object handles and I don't know of any other way to get the listener handles (although I've only searched briefly).
[addendum]
To add context to my previous comment that brought this 5+ year thread back to life, I am (was; partially gave up) trying to troubleshoot this problem:

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

回答 (2 件)

Harry Dymond
Harry Dymond 2020 年 7 月 3 日
Found the answer on StackOverflow: any object with listeners attached will have an undocumented property .AutoListeners__, which is a cell array of listener handles. The cell array does not appear to get "pruned" if listeners are deleted, so .AutoListeners__{x} could contain a handle to a deleted listener. If the object has never had listeners created for any of its properties, .AutoListeners__ will not exist.
  2 件のコメント
Adam Danz
Adam Danz 2020 年 10 月 15 日
Good find, Harry Dymond!
DNF
DNF 2021 年 4 月 27 日
As far as I can tell this does not return all listeners. Listeners to some undocumented events do not show up. If you attach a listener to a window for its WindowMouseMotion event, then you cannot find that in the .AutoListeners__ array.

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


Joakim Magnusson
Joakim Magnusson 2014 年 8 月 14 日
Can't you save your listeners when created like this? :
handles.listener1 = addlistener(...);
handles.listener2 = addlistener(...);
etc...
Or that wont work for you?
  2 件のコメント
Daniel
Daniel 2014 年 8 月 14 日
Thanks, but unfortunately no.
This is exactly what I wanted to avoid. I don't want to pass the variable 'handles' across my functions and methods, as this would force me to a severe code rewriting and restructuring.
I have the impression that listing all objects listening to something should be easy (that is what Matlab itself does when broadcasting a "notify"), but looks like the program does not offer this functionality to users.
Joakim Magnusson
Joakim Magnusson 2014 年 8 月 14 日
No i couldn't find anything like that either.

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

カテゴリ

Help Center および File ExchangeEvents についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by