LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Stopping a for loop inside an event structure vs. producer consumer

Hi,

 

I keep hearing how it's bad coding to place loops within an event structure however I am not understanding how else I may accomplish my task.

 

So basically the code I have now cycles through 16 different measurements in a for loop with parameters changed on each iteration. I'd like to have a stop button also that halts the measurements but most suggestions I have researched on the forums indicate that I should change my program altogether in order to do this and use something like the producer consumer setup.

 

Any suggestions other than shifting to producer consumer? I'd rather not change the program with the loop inside the event, as it is pretty much large and has 3 different event cases already coded up and I also don't fully get producer consumer anyways. It seems like a pain to change any overall programming architecture now and not worth the effort since I am almost done creating it.

I can attach my code if needed - so please let me know if that is necessary.

 

Thanks

0 Kudos
Message 1 of 8
(3,349 Views)

Yes, please show us your code.

 

You can easily use the outer while loop and the timeout event to cycle through your measurements. No need for inner loops.

0 Kudos
Message 2 of 8
(3,348 Views)

Hi Altenbach,

 

Thanks for the quick reply. Unfortunately I can't post the code because part of it is owned by a company. Do you know if you can post a simple program which has a number of events, one of which corresponds to a for loop using the producer consumer design?

 

I still can't wrap my head around how to do this with the producer consumer design. Would the for loop go in the consumer or the producer loop? And if it does go in the consumer what about the other events which don't involve a for loop??

 

Thanks

0 Kudos
Message 3 of 8
(3,296 Views)

Also how does this solve the problem of being able to stop the program in the middle. If there is a for loop in the consumer loop wouldn't it continue to iterate instead of stopping?

0 Kudos
Message 4 of 8
(3,292 Views)

@AKCanada wrote:

Also how does this solve the problem of being able to stop the program in the middle. If there is a for loop in the consumer loop wouldn't it continue to iterate instead of stopping?


I did not suggest to use a FOR loop. You can use the outer while loop and keep track of the state. Triggereing a stop event will be able to interrupt at every iteration of the loop.

0 Kudos
Message 5 of 8
(3,283 Views)

Hi Altenbach,

 

Aha ok I think I see what you mean now. So this would take place in the producer loop I am assuming?

 

Now what if I'd like for there to be two options of stopping the loop - so one in which it finishes 16 iterations and another option in which you manually stop it? In order to incorporate the first option how could this efficiently be done?

0 Kudos
Message 6 of 8
(3,271 Views)

Hi Altenbach,

 

I really like that idea and the code you linked out to! I was wondering however if this may be used for multiple events and if it can branch out to a consumer loop to be the basis of an entire program? Or is this structure more a simple way of doing a specific task and may have limitations to it being used as the core of a larger program?

 

Thanks

 

0 Kudos
Message 7 of 8
(3,230 Views)

This would be a great time to watch a 13 minute demo

Also do check out the documentation for the project template.  You will understand what that implementation of a Producer Consumer loop does and how to use tthose te3mplates to get up and running FAST!  The last time I whipped out a "Demo" for my favorite NI Rep I had one of those templates up and running in about 15 minutes on simulated equipment.  The customer did decide that the NI solution wouldn't pose a problem overcomming the "LabVIEW Learning curve" since the solution arrived before the PO was cut.


"Should be" isn't "Is" -Jay
Message 8 of 8
(3,210 Views)