LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to get out of deeply nested structures

Solved!
Go to solution

I'm relatively new to LabVIEW and I keep getting into this situation:

 

when the tasks at hand get a little more complicated, my programs use a lot of nested structures like loop, case, and sequence structures. Now, how does one exit nested structures quickly and cleanly? In a programming language like Java you could throw an exception, but in LabVIEW I'm apparently condemned to use even more case structures to check for error conditions so I can skip and get out immediately when an error has occurred or the abort button was pressed.

 

I feel I'm doing something terribly wrong. Any input on how I can structure my programs better to avoid this?

 

Thanks in advance.

0 Kudos
Message 1 of 6
(2,741 Views)
Solution
Accepted by topic author someguy

Hi someguy,

 

ever heard of "state machines" or "producer-consumer pattern"?

 

You will find a lot of information here in the forum...

 

When you are new to LabVIEW: you will also find a lot of free resources on NI's website for learning LabVIEW!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 6
(2,738 Views)

Hi,

 

An example would be good in this case, can you post one?  

 

Generally speaking, if you are using a lot of case structures and nested loops, from a software development point of view, I would say the program is not being planned out. Maybe Smiley Happy

 

Again, in general terms, you should use a state machine with lots of cases that are not nested, so that when you do a specific piece of work you leave the case and are back in the main loop.  If there are a lot of cases, and it sounds like it, then you should try a queue driven state machine, that way you have more control over what case comes next.

 

Also, you could try a producer consumer, which is queue driven, to more cleanly program the code.

-------
Mark Ramsdale
-------
0 Kudos
Message 3 of 6
(2,733 Views)

Throwing exceptions is generally not something you should be doing anyways.  But to reiterate what has already been said, learn to use state machines.  There are examples all over the place.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 6
(2,721 Views)

what i like to use is the "notifier operatons". whats neat about it is you can use it for clocking your loops using the timout node and use the timeout bool to stop the loop when you send the notification messege...just make sure you create a control terminal for the "notifier" at low level inorder to recieve the messege from the top level VI.Smiley Wink

abort notifier.png

0 Kudos
Message 5 of 6
(2,696 Views)

Thanks for all your input! It's greatly appreciated.

 

I've switched to a state machine now. Everything works fine. Code is much cleaner.

 

The "notifier operations" thing also look interesting. I'll study that later.

0 Kudos
Message 6 of 6
(2,656 Views)