Slider ghost events and how to avoid them

One typical UI problem a LabVIEW developer faces is how to prevent an oversupply of events generated by some front panel objects, particularly sliders and knobs. When the user drags the slider, a lot of events with slightly different associated values pile up waiting to be processed. So if each event entails a complex code execution or communication with some equipment, we are wasting time and resources with this "ghost events".

As you may know, this is an old problem and any search on NI forums will uncover several workarounds. In my experience, the best approach is to validate the event before processing it. But how?

As in the image above, first wait a length of time and then check if the event associated value is still the value in the control. If not, discard (because the final value is still waiting). Sounds simple but some considerations are required:

First, the milliseconds to wait (500 in this example) will both filter a higher number of unnecessary events and delay the execution of the actual ones. We must balance both aspects.

Second, our code must not lock the front panel (or the UI would be unresponsive while we discard spurious events) and can't allow a queue of events piling up waiting to be discarded. The easiest way to achieve this is unchecking the "Lock panel until the event case completes" option and checking "Limit maximum instances of this event", both at the bottom of the event configuration tab, as you can see in the next image:

And that's all, happy coding!

Comentarios

Entradas populares de este blog

SNMP library for LabVIEW

Base64 Encoding

Retrieve CPU temperature