LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

stopping while loop in subvi

Something to bear in mind about globals and the refrain "...well for this application they work..." is that you need to have a complete understanding of the word "works". In a professional application, this means far more than simply satisfying a functional requirement. The larger and more important question is do they "work" in terms of supporting maintainability? Do they work in terms of extensibility? Do they work with reliability? Do they enhance the code's readability -- and all the other "-abilities" that good code needs to exhibit.

The problem is that globals are too often thrown in as a replacement for thought and careful design by people who don't think they have the time to fully understand the task they are trying to accomplish. Too often the result is code that stops working because something unforeseen (but foreseeable) arose that changed the timing or something else just enough to cause problems. Upgrade LV or a driver, change a piece of hardware to a compatible device, or even install a CPU-intensive application that has nothing to do with your test program and suddenly a program that had appeared to work "for years" suddenly breaks. Combine this effect with the tendency for people to assume that their code will never encounter (or generate) an error - and you have a recipe for a lot of long nights spent drinking bad coffee.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 11 of 16
(1,330 Views)


@smercurio_fc wrote:
I've said this before, and I'll say it again: GLOBAL VARIABLES ARE NOT EVIL.

I agree. However, in this particular application, a queue is already in use in the sub VI. The more appropriate solution is to simply move the already existing queue into the main VI so that both can access it and stop the program. Adding a global into the mix will only complicate things.

I certainly don't completely disregard globals, but when there is a better solution, I will use it over a global any day.

0 Kudos
Message 12 of 16
(1,318 Views)
Marc A,

I am on your side Smiley Very Happy Good talk Smiley Very Happy

CLAD
Certified LabVIEW Architect
Certified TestStand Architect
0 Kudos
Message 13 of 16
(1,311 Views)
Yet another approach would be to use an event. Pass a reference to a shutdown user event and tugging on it anywhere brings the application down in a nice orderly manner. You can even design the event data to hold the name of the VI triggering the shutdown to provide traceability.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 14 of 16
(1,305 Views)
I simply refuse to get involved into yet another debate over where/when/if global variables should be used. The merits/demerits of them has been discussed before ad nauseam. Needless to say, as is evident from responses like the one from ohiofudu, there will be cheerleaders in the "globals are bad no matter what you do" category. Whatever. I know how they work, and I do not advocate the "always use" line because I'm well aware of their pitfalls. That means I use them when I know they are quite appropriate for the situation. Consider the simple example VIs that I mentioned in my response that are available from the Example Finder. Are you going to tell me that the use of a global variable in that instance is "bad"?

I've been using LabVIEW since version 2, when globals didn't even exist. When they came out in version 3 everybody said, YEAH!. Then, the abuse started, and the "global variables are evil" mantra started as well. I believe our responsibility as "experienced" LabVIEW users is NOT to state simple generalizations like globals are evil. Rather, it is to provide various solutions to problems and then discuss any pros and cons. The user asked for a mechanism to stop a subVI. I provided one. Marc provided another one. Which is the better approach? Without knowing the intent of the application, or the overall design of the application, how are you supposed to answer the question, and who's to say that the solution provided by Marc is "better" than the one I provided? "Better" based on what criteria? Is it better to use an error condition to stop a loop, like the destruction of a queue, or is it better to use a control signal like a global variable? I am going to assume the user is intelligent enough to regard the solutions and over time learn what's best for each application, taking inputs from sources like their own learning process, trial and error, and inputs from this community that are more informative than simple cheerleading.

0 Kudos
Message 15 of 16
(1,299 Views)

Well said. I don't want to get into a debate either. I'm a newcomer to LabVIEW compared to you (about 2 years experience) and I don't for one second doubt your knowledge.

My criteria for "better" in this particular example was the fact that he had already implemented a queue to control 3 while loops. Since it was already there, he might as well take advantage of it. Depending on the situation, a global could be a very viable option. I only said I avoid them because I personally don't like working with them.

0 Kudos
Message 16 of 16
(1,295 Views)