Page 1 of 1

Attached Status Change Before or After Calling onAttach Handler?

Posted: Fri Jan 31, 2020 10:29 am
by peterpolidoro
Hi. I am curious, does the attached status returned by getAttached() change before or after the onAttach handler gets called?

I would like to call some function once all Phidgets in my system have been attached. This is easy if I use openWaitForAttachment(), but I am curious about how to do this without blocking code.

Is there a way I could do this with onAttach handlers? If I attach the same onAttach handler to every Phidget, then each time it gets called I can check to see if all of the other Phidgets have been attached. The check would fail for all onAttach handler calls except the very last one called by the last Phidget to get attached.

This will not work, though, if the attached status changes after the onAttach handler returns. The very last Phidget to get attached will not say it is attached in the getAttached() call within the onAttach handler.

Is there a better way to asynchronously trigger some function call once all Phidgets in some set have been attached? Thanks!

Re: Attached Status Change Before or After Calling onAttach Handler?

Posted: Fri Jan 31, 2020 1:37 pm
by Patrick
getAttached() will return false if called from within the attach handler. This is so that something polling attached will be guaranteed to wait until after the attach event finishes.

The attach event fires as soon as you can communicate with the device, and is meant for setting your default settings. You may or may not be able to read out sensor values, etc. from within the attach handler.

openWaitForAttachment() waits for the attach event to get fired and return, and then also waits for the device initial state to come in (sensor values, IO states, etc) before returning, so that you can just open and poll a sensor, etc. immediately.

You could just maintain an up/down count on attach/detach events to wait for your # of attached devices to reach the expected amount.

-Patrick