10-30-2008 10:22 AM
I have a main vi that has an event structure and is used to call several sub vis that have their front panels displayed on a sub-panel on a tab page. The sub vis are called using the VI>Run VI invoke node and use a Static VI reference. Some of the sub VIs contain while loops and run indefinitely until the main application is closed. I need to close these sub VIs neatly (not just use the abort node).
I have two solutions: One uses property nodes Panel>Controls to supply an array of the control references in the subVI. A loop is used to index the array to find the reference for the 'Stop' control and a boolean high is written to stop the sub VI. This is duplicated for each subVI (though could be a single occurrence and indexed). The second solution uses a single global variable, written to once in the main VI and read once in each subVI.
I know using globals in many situations is considered poor practice, and to-date i have followed the mantra that globals should be avoided unless there is no other way. Considering the amount of unintuitive code this approach is generating (given invoke and property nodes hardly constitute easy to read data flow apart from the reference and error wires), should i be looking at taking a softer line on variables?
Of course it has not escaped me that there may well be a simple non-variable based answer....
Being fairly new to LV, the most reoccurring problem i have is knowing which is the best solution, when there are several alternatives to choose from. ...and yes i have forums and books and tutorials and manuals coming out of my ears...
10-30-2008 11:10 AM
10-30-2008 11:18 AM
Thank you for demonstrating the problem with the anti-globalist movement. It's not that globals aren't a potential for problems. It's just that there are cases where they are valid (most often when there's a single writer) and repeatedly telling people "DON'T USE GLOBALS!" results in people coming up with much more problematic solutions, as you've found out yourself.
In this case, the global is definitely the better approach.
Another approach I like better is using a simple LV2 style global to do this, because it allows every loop to stop the sequence. Of course, that's not always appropriate for the architecture. My simple version has two inputs (reset and stop) and a single output (stop) and is placed before the conditional terminal. Any loop setting stop to T causes it to be T for all the other loops. There are other versions which use names, so you can have groups of loops, etc. but it all depends on what you need.
Other options include notifiers, queues and dynamic events, but the global is usually the simplest.
10-31-2008 08:59 AM
Thanks for the comments, Wiebe -that sums up my situation, like a child in a sweet shop which do i choose?
...gotta learn more. Though there is a certain appeal of Henry Ford's approach to consumer choice.
tst, thanks for the input, good to read. It aligns with my perspective, -though that is something i am still evolving. I think the LV2 approach would work well for the small amount of data in my app, as well as the stop flag. Thanks for the link, always great to have them pointed out, dredging can be a bit hit and miss.
atb
Blue
Where's the Spanish Inquisition..... ?
11-01-2008 11:31 AM
My problem has always been that people say "locals/globals are evil" and then there are two bad options:
As for the inquisition, they have been retired for quite some time, and I guess I must be growing old, since the Brain has been around for a long time already without me feeling like replacing him.
11-01-2008 02:51 PM
I agree with tst.
Hey Wiebe, what is a "global buffer " ?
11-06-2008 04:10 AM