LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Can 2 while loops work at the same time?

Solved!
Go to solution

I have two while loops in my program; one reads data and the other does nothing until I hit stop on the first one. I want the first one to always run and the second to run off of the time constraint I have for it when I hit the start button. I am using a DAQ and transfering data from one while loop to the other. See attached file.

 

Main questions:

How to attach the start button?

How to get the second loop to work with the first?

 

Thank you

0 Kudos
Message 1 of 11
(8,519 Views)
Solution
Accepted by topic author Relient

Your problem is that the loops have a data dependency.  If you want to run both loops at the same time, they can't have any wires running from one to the other.  Look into the Producer/Consumer architecture.  It uses queues to send data from one loop to the other.


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
Message 2 of 11
(8,516 Views)

In your code one loop output feeds the next loop. This is a data dependency.

 

For small applications or simple constructs you could also cheat a bit and use local

variables to pass data from one loop to the next as they both operate simultaneously.

 

Just be aware that local variables can introduce race conditions if not handled with care.

You are responsible to insure this does not happen. 

 

Just be very careful to

1)Assure proper initialization of the local first before using them anywhere in your VI

2) Insure no two code sections write to a local variable at the same time. This is your responsibility.

 

3) reset the locals before exiting the VI

 

A queue would handle most if not all of this responsibility for you  but slightly more tedious to set up and master.

If you have little time you can always cheat to get what you need at the expense of

scale ability later.

 

Its your choice but. Thats why LabVIEW is so popular.

 

 

 

 

 

 

 

Message 3 of 11
(8,500 Views)

Let me speak from experience.  Locals are very rarely ever the way to go.  And this is not one of them.  You will run into race conditions and you will lose data.  And queues are not difficult to use.


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
Message 4 of 11
(8,493 Views)

Thanks again, I got it to work easily by using the producer/consumer pattern.

0 Kudos
Message 5 of 11
(8,481 Views)

I am learning labview as I go and little errors keep stumping me. I set up the producer/consumer design and it reads the two while loops like I want. However, when I stop the program to save it to the write to measure it gives me an error saying invalid input and highlights the dequeue. I have to queues running (one for a butotn and one for data) and they both do this. I tried connecting it different ways but its always the same error. Any ideas? 

 

0 Kudos
Message 6 of 11
(8,418 Views)

Since your lower loop has a dequeue with no timeout (-1), it is waiting for an element. If the queue is destroyed before it gets one, it throws an error. Since you don't have the error output of the dequeue wired to anything, automatic error handling in labview kicks in and pops up the error. You can actually use this to your advantage. Instead of the second queue to handle the button, place a case structure around all your code in the second loop that comes after the dequeue. Wire the error output of the dequeue to it so that code only fires when there is no error from the dequeue. Also branch the error wire straight to the while loop conditional terminal. This will automatically stop the lower loop when the queue is destroyed. This should eliminate the need for all the hidden stop buttons.

Message 7 of 11
(8,404 Views)

1. Create a stop Control on the Stop condition in the first loop.

2. Create a Local Variable of the Stop Button in the first loop

3. Put the local variable  the second loop and wire it to the stop condition.

Voila. You fooled the system xD

I also struggled with this problem

0 Kudos
Message 8 of 11
(4,873 Views)

Hi darkieboy,

 

congratulations on solving a 7 year old "problem"…

 

3. Put the local variable the second loop and wire it to the stop condition.

Voila. You fooled the system

Yes, sure you "fooled" around…

 

What happens when you want to refactor your VI and move that second loop into its own (sub)VI? Now you fooled yourself!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 9 of 11
(4,869 Views)

I started working in LabView 4 days ago. I simply need a solution for this. I read about problems with this thing, but for a simple application is enough. I did not studied the queues. 

Thank you. It is also my first contact with this type of programming. It s so different from the traditional text based(C/C++)

0 Kudos
Message 10 of 11
(4,865 Views)