09-22-2022 03:48 AM
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
09-22-2022 04:47 AM
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.
09-22-2022 09:08 AM - edited 09-22-2022 09:13 AM
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
09-22-2022 09:34 AM - edited 09-22-2022 09:35 AM
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.
09-22-2022 10:06 AM
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.
09-23-2022 02:32 AM
i will implement this and let you know
09-23-2022 09:58 AM
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.
09-23-2022 11:06 AM
@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.
09-27-2022 09:10 AM - edited 09-27-2022 09:11 AM
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.
09-27-2022 09:48 AM
aputman, can you give me link of example so i can understand it in better way?