LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

while loop inside the event structure

Hi everyone,

Im trying to use while loop inside the event structure and im trying visa read and write operation. im attaching  the sample vi with loops and events. In event 1 when i click on button the visa is reading and writing once but i want make run while loop continuously and run visa read and write continuously. how can i make it? Please give me some suggestions

 

thank you in advance 

0 Kudos
Message 1 of 15
(1,355 Views)

The usual way is to separate your GUI (event) loop and the DAQ loop...

 

You can make it 'work' in one loop, if you do your read outside the event, but in the event loop. Then, add a time out to the event, so the read is executed even if there's no user event. This isn't very reliable nor saleable. It can work for the most trivial problems, but when you start adding things it gets messy.

 

Look into producer\consumer and\or master\slave architectures. Some would use AF or (D)QMH for this... But some 'architecture' is needed for this.

Message 2 of 15
(1,339 Views)

thank you for the suggestion.. i can explain my problem further so you can understand it better.  Im going to add more things in it.... Multiple cases That while loop should work continuously with pause and continue while loop and number of iterations as i modified in new vi which is im attaching now. so the idea is event 0 should execute 1st only once when button is pressed or even then event 1 is execute continuously like a while loop. i cant use parallel loops because i used it and i was getting errors so i have to use all visa read and write from event 0 and event 1 serially i as did it in sample VI. Can you or someone  else give me suggestions ? i looked into forum and checked some questions there its mentions its not good practice to use while loop inside the event... so how can i recontruct it so that event 0 execute only once and event 2 or while loop in event or separated diagram from event 2 run continuously? 

thank you in advance 

0 Kudos
Message 3 of 15
(1,309 Views)

This is where you would implement a Queued Message Handler, where the user interaction will Queue certain actions to the message handler loop, and then from there the message handler loop will take over and perform the tasks appropriately.

 

https://www.ni.com/en-us/support/documentation/supplemental/21/using-a-queued-message-handler-in-lab...

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
Message 4 of 15
(1,297 Views)

The JKI state machine would also work well for this.  The idea is to keep long running loops out of the event structure.  With the state machine architecture, your event structure will activate a state that will perform your tasks.  And rather than running a while loop in the state, the idea would be to call the same state over and over until the task is complete.  This way you can continue to monitor your UI and respond appropriately to other commands that may come down. 

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
Message 5 of 15
(1,289 Views)

i will implement this and let you know 

0 Kudos
Message 6 of 15
(1,230 Views)

Having a While loop inside an event structure isn't a problem in itself, having code that takes a long to execute is ... probably.

If it's ok that the UI 'freezes' while working i'd say it's ok, but you should add a Cursor Occupied so the user knows that it's working.

If you want it to do some series of commands that might need execution branches, then a State machine with the event structure in the Idle state is good solution.

If you need the UI to be responsive while executing, then you need to separate it into some other branch, a queued loop being the most common, but it could also be a spawned process or a message to a worker thread.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
Message 7 of 15
(1,213 Views)

@newmemeber123 wrote:

Im going to add more things in it....


A good reason to abandon the approach that you're taking.

 


@newmemeber123 wrote:

so the idea is event 0 should execute 1st only once when button is pressed or even then event 1 is execute continuously like a while loop.


This sounds like a state machine.

 


@newmemeber123 wrote:

i cant use parallel loops because i used it and i was getting errors so i have to use all visa read and write from event 0 and event 1 serially i as did it in sample VI.


Please explain further what kind of errors that you were getting. Many on this board use parallel loops all of the time and could explain why you're getting the errors. 


@newmemeber123 wrote:

i looked into forum and checked some questions there its mentions its not good practice to use while loop inside the event...


Putting a loop inside the event structure will lock the event structure until the loop completes. There could be situations where this would be OK, but I have never found this to be the correct approach. If I need a loop I will send a message from the Event loop to the loop that has the code that you want to run. Study about the Producer/Consumer or Queued Message Handler architectures. If you don't need to have UI interaction while you're running the process a single loop State Machine with the Event structure in the Idle state would be adequate.

Message 8 of 15
(1,204 Views)

hello sorry for late reply... im attaching the VI the parallel loops with procedure and consumer architecture but im not getting how to queue the visa resource name out in a queue so the 1st loop will execute once and then next while loop execute continuously.( commands in 1st loop are initialization  commands for 2nd loop that is the reason it should execute once and measurements in the 2nd loop measure continuously. ). waiting for suggestions. 

 

 

0 Kudos
Message 9 of 15
(1,121 Views)

aputman, can you give me link of example so i can understand it in better way?

0 Kudos
Message 10 of 15
(1,109 Views)