LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Nugget of the week: Dynamic event registration

This weeks nugget is about dynamic events, the way to use them and how we could use them.

First about the event structure:
Basically the event structure is a piece off code that doesn't follow dataflow rules.
When it is executed, LabVIEW waits on an event and then performs the action specified in that event-case.

Normally these events are user based, like keyboard actions (up, down), mouse action (up, down, move).
Event based programming can be tricky but it is usefull to get the event structure 'in your fingers'.

Dynamic event registration allows you to turn (parts) of your events on and off.

Let's say you have a program that allows you to set points in a picture control. Like a pixel colorizer in Paintbrush

With polling it looks like this:

(don't run this code! it has no correction for position of the Picture control or even a delay on the while loop)

With an event case the code looks like this:

With some additional events like mouse-down and some status control we can skip the mouse-device VI's.
But it means the 'Mouse Move' event is triggered every time the user moves the mouse inside the picture control and can cause an overhead on the program.

Dynamic events allow you to turn the events on and off so the event only activates when the user has the left mouse button down inside the picture control (or part of that control).

See this:


The actual event that colorizes the pixel [0] is initially referenced to a static non existing 'Ref constant', but it will become active when the mouse is going down (eventcase [2])* and is turned off again if the mouse button is released or the mouse moves out of the picture control.

With dynamic events you have a very powerfull tool to manage your event node! And reduce calls to your event node.

You can pass the the event refence into subVIs and even Action Engines,
but be aware that you can best store them in a strict type def control
since the data type must be preserved!


To have a look in which direction event nodes are going I advice some study on XControls.

A list of all Nuggets can be found here.
If you have a Nugget you would like to present please post here to reserve your week!

I'd like to thank Darren for his effort into starting the nuggets!
My thanks to Ben is expressed by using the picture control Smiley Wink

Ton

* Notice that I don't check which button is down! This should be a next improvement



Message Edited by TonP on 04-17-200710:38 PM

Message Edited by TonP on 04-17-2007 10:42 PM

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
Message 1 of 77
(35,293 Views)
Nice nugget Ton 🙂 One correction, you forgot to unregister you events in the end...

I'd like to empasize something many users seem not to be aware of since dynamic events are poorly documented and the only way to find information on how they works is hands on testing. Dynamic event registration creates something called "Event registration refnum". According to my tests this refnum seems to act as if it was a "mailbox". Event structures can handle messages in this mailbox. Each time an event structure handles a message in the mailbox, the message is removed from the mailbox. Even though event registration refnum can be wired to multiple parallel event structures, I strongly recommend not to do this. Always use an event registration refnum on a single event case only. Sharing event registration refnum would be similar to sharing a mailbox with a neighbourgh. If your neighbourgh takes your messages from your mailbox then you don't get them. The same happens for event structures. If one event structure reads an event from event registration refnum, then other event strucures using the same event registration refnum don't get this event any more.The correct way to handle same event is multiple event structures so that each event structure handles the same event is to register the event separately for each event structure.

User events are a nice tool for asyncrhonous messaging, but that's a topic for another nugget, or perhaps an expressionflow article 🙂

Tomi
--
Tomi Maila
Message 2 of 77
(35,272 Views)


@Tomi M wrote:
Nice nugget Ton 🙂 One correction, you forgot to unregister you events in the end...

Hi Tomi,

I was not fully sure this was necassary, I thought this only was needed for user events because you have to destroy those.

One thing to add:
It is valid to have multiple event structures listening to the same event (but not to the same dynamic event registration refnum)

Ton
Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
Message 3 of 77
(35,263 Views)


Hi Tomi,

I was not fully sure this was necassary, I thought this only was needed for user events because you have to destroy those.


I beleave LabVIEW keeps sending even non-user events to event registration refnum "mailbox" as long as you have unregitered them or the main VI exits. This consumes both some processor power and memory and is not a good idea if you are not going to handle those events ever. So always unregister event registration refnum when you no longer need it. This is often done after the while loop containing the event structure exits.
--
Tomi Maila
Message 4 of 77
(35,259 Views)
Hi Tonp,

Nice Nugget I have always  done my dynamic event this way.
keep the Nuggets coming.Thanks once again.

CLAD
Certified LabVIEW Architect
Certified TestStand Architect
Message 5 of 77
(35,245 Views)
Thanks Ton,

I never understood dynamic event registration.  In fact, I still can't claim to fully understand, but my lack of understanding has been significantly reduced by this nugget.  The assignment of constants (non-existant) versus object references was the part I never grasped before (If indeed I have grasped it now).

I might have grasped it a bit quicker (I had to read it two or three times) if the case structure 2 "Mouse down" assigned a reference from the Picture control (As in other cases) instead of from the event case "CtlRef".  I mean, the picture control is the only one registered for the mouse down anyway, right?

I'll need to try this out, being a fresh 6.1 convert and all.

Thanks again.

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
Message 6 of 77
(35,151 Views)
Hi again,

I think a bit more understanding of this topic is trickling slowly into my de-caffeinated brain.

Assigning a invalid control reference ("Ref constant" in your example) disables the event, right?  I think this disabling could perhaps be done more intuitively, but whatever.  Does the type of the reference even have to be the same as the enables states, or does it just have to support the event?

This also enables passing control references into sub-VIs and delegating event-handling of higher-level VIs right?  I remember looking at a NI example of this, but it frightened me because there's normally not much G I don't understand, but it truly baffled me.  Like the first time I tried to use occurrences.

This really is a powerful tool, no?  Moving certain event-handling to a sub-VI offers whole new opportunities really.  How about efficiency, any difference in performance versus "standard" events?

Shane.

Message Edited by shoneill on 04-18-2007 11:23 AM

Message Edited by shoneill on 04-18-2007 11:23 AM

Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
Message 7 of 77
(35,125 Views)

Hi Ton,

This really is a ton of stars worth ! Smiley Very Happy

I dont know at all about Dynamic Events since I ve never used these features in my work yet.

Hope to do soon after studying this nugget completely.

A Nugget in need is a Nugget indeed ! Smiley Wink

- Partha ( CLD until Oct 2021 🙂 )
Message 8 of 77
(35,062 Views)

Nice nugget. I like using dynamic events to create UI handling subVIs for common UI functionality. You just have to supply them with a control reference and a boolean stop event and they can run as a seperate process.


@Tomi M wrote:

Even though event registration refnum can be wired to multiple parallel event structures, I strongly recommend not to do this. Always use an event registration refnum on a single event case only. Sharing event registration refnum would be similar to sharing a mailbox with a neighbourgh.

It's also buggy.


___________________
Try to take over the world!
Message 9 of 77
(35,034 Views)
To be honest, I'm still a little unclear about how dynamic events work.

I guess I'm uncertain as to what events would ever need to be handled in a subVI and why.

Is that so different from inserting a subpanel?

Thanks!
Wade
Message 10 of 77
(35,017 Views)