Polling vs. Events

From Phidgets Support
Jump to: navigation, search


The Phidget library operates asynchronously. The library fires attach and detach events when a device channel appears and disappears (plugged in or unplugged). After a user channel is attached, data and status updates from the device channel begin being delivered. These events cause the library to update properties of the user channel, and to call any event handlers set by the program.

Discussion

Attach

Once a channel is opened, the Phidget library begins attempting to match the channel to a device channel. While the channel is not attached, the Attached property of the channel will be false. As soon as the Phidget library matches the channel with a device channel the Attached property is updated to be true. A program could poll the Attached property to determine when the channel becomes attached, or set an attach handler to be notified asynchronously.

Detach

While a channel is open and attached, the device channel could disappear for some reason (could be unplugged). As a result, the Phidget library would detach the user channel, and update the Attached property to be false. A program could poll the Attached property to detect when the channel becomes dettached, or set a detach handler to be notified asynchronously.

Data

While a channel is open and attached, data and status updates received by the Phidget library are delivered to the channel. A program could poll data and status properties (for example, Acceleration, Temperature, and State), or set handlers that would be called when the data or state changes.

Recommendations

Generally, setting event handlers is preferred and the most flexible. The code required to poll for attach and detach states, and to detect data and state changes can be complicated and error prone. Event handlers are simple and work in a very predicable manor.

A simple program that only wants to read a small amount of data from a device could simply open a channel and wait for attachment, read a data or state property, and then close the channel. In that case, the code to implement and set event handlers might not be worth the effort. Error checking however becomes very important, as accessing some channel properties while the channel is detached will result in an error or exception that could kill the program. There will always be a race between checking the Attached property and accessing a data or state property.

All but the most simple program should set handlers that will be called when a channel attaches or detaches, and when the channel receives data or changes state. The channel will only receive change events while attached, and no external calls are required to access the changed data, which eliminates a chance for error. In addition, change triggers and data interval settings will not get reset accidentally if they are always set from within an attach handler, eliminating another common mistake.

When in doubt, use event handlers.

Further Reading

Phidget Programming Basics - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.

Data Interval/Change Trigger - Learn about these two properties that control how much data comes in from your sensors.

Using Multiple Phidgets - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.

Logging, Exceptions, and Errors - Learn about all the tools you can use to debug your program.

Phidget Network Server - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.

Best Phidgets Practices - Good programming habits that will save you from common problems when writing code for your Phidgets.