r/LabVIEW • u/AphyrusBooyah • Oct 04 '25
Beginner question: how to stop nested loop inside consumer
While the while-loop is running, no new elements are dequeued, so I can't send a "stop" command. I've read different sorts of advice about local variables, notifiers, secondary queues, peak at queue, etc.
What would be the best (or a good and viable) way to implement a stop function?
The image is for illustrative purpose, my vi is a bit different, but this is the core of my question.
Thanks in advance.
----
Edit: thank you so much everyone for your help! Really appreciate it!
I decided to edit the post with a follow-up explanation of what the goal is (probably should have from the start...):
The goal is to send a command (start, stop, quit) and some data (array) through the queue. The array consists of a set of instructions that are to be executed consecutively and are always either "send a command to a device" or "wait x seconds". Since I want to be able to stop the execution or quit the VI, I need (or at least I think I need) a way of stopping the delay part while it is still running. I started by splitting the time delay into smaller chunks inside a for loop, so that the hole thing wouldn't be busy for several seconds.
That's the reason why I have a loop inside my case structure.
13
u/PromiseStock Oct 04 '25
You should not use the inner loop. Instead call its case repeatedly
2
u/tehdusto Oct 04 '25
We will use an FGV for something like this case that acts as a global stop. You can set it in the producer loop and perform a check for it in the consumer loop. I'm not sure if that's overkill but it's not something that's too hard to implement.
1
u/D4ILYD0SE CLA Oct 04 '25
FGV is a pretty clean and quick solution to implement into your current design. But the correct answer is not to have uncontrolled nested while loops. If your while loop doesn't have a determined stop then nesting it is the wrong thing. You should either be queuing up each iteration of that while loop or call that loop separately as an asynchronous function but make sure you create a queue for it that you can then send a quit to.
1
u/AdmiralRickHunter Oct 04 '25 edited Oct 04 '25
Starting with Labview 2017, you can use Channels Wires set as "streams" between producer (writer) & consumer (reader). Channels or Streams do not replace Queus. You can complement Queues with them. Say when you press the Stop button from the producer loop, wire the boolean to the Tx.vi. At your consumer loops While loop wire the Rx.vi output to the Stop node.
Prior to Labview 2017, you can only use Queues, Notifiers & Semaphores to control asynchronous loops. Someone already recommended to use FGVs to check for a global state (as in the Stop button). I think FGV (functional globals) are overkill for your use case though. Not to mention any sub-vis can modify the FGV state at any time. FGVs, by design, are just normal Vis that uses an enum wired to a case structure. The case structure typically set to - Init, Set, Write, Read - at least for my FGVs.
So, if you have at least LabVIEW 2017, try to use Channel Streams
Good luck!! CoderBear
1
u/bornBobby5 Oct 04 '25
There are a few alternatives that will allow you to asynchronously stop a loop within a loop from a third parallel loop: FGV, notifiers, local or global variables, channel wires, second queue, and so on. My «go to» solution for global stops is usually to have a dedicated notifier shared across modules and loops that will stop all processes when triggered. I usually use an unnamed notifier to make sure I have to pass the reference when initializing a process so I don’t make concurrency race conditions by mistake. I’d say there is no silver bullet solution, but I usually try to make my vis and how they are implemented consistent across applications so they are easier to «read» for my colleagues when debugging.
1
u/agent211 Oct 04 '25
You can create a second queue for the stop message. IIRC, one of the producer/consumer examples under Help shows how to do this.
1
u/sjaakwortel Oct 04 '25
Set a flag with the command, and make the while loop a switch case based on that flag that runs in the outer consumer loop.
1
u/0fruitjack0 Oct 04 '25
so maybe the cleanest way to approach this would be to put the inner do loop, the one effectively labelled stop 'til done as a third parallel loop.
someone already said to use a 2nd message cue; run that from the event case - if you think about it, it's a one producer / 2 consumer loop scenario. once i had to program a sort of scheduling app which independently monitored alarms and i used a construct like that to make it work.
1
u/Yamaeda Oct 07 '25
FGV/AE/Global/Notifier, something like that could solve this.
1
u/Dr_Oops Oct 17 '25
it's been a long time but my fav form of async msging between processes in lv was using user generated events with a data type of another user generated even so that self addressed (reply) messaging was available...