11-15-2022 04:43 AM - edited 11-15-2022 04:46 AM
Hello! I'm not a professional programmer, but work with LV occasionally since more than 15 years. In a current tool I almost finished I did something I never did before and now have difficulties to understand how LV works at that point and how to solve it. Following setup:
The problem: even if both loop exits receive the TRUE signal at the same time, they won't exit. I have to trigger the "Exit" event a second time or trigger any other event. Why is that? The event itself is finished and the event structure has been left. The probes show me both exits got TRUE.
Side question: the one loop with the event structure contains nothing else. After having started the program I cannot halt the loop with a halt point. It would only sort of go into that loop when an event is triggered. Why is that?
Solved! Go to Solution.
11-15-2022 06:05 AM - edited 11-15-2022 06:06 AM
Hi,
you missed to attach your code so I have to assume what you did.
I think you read the boolean outside of the event structure. When code execution enters the while loop all code that can run will run. I assume the event structure waits for an event and the boolean variable outside the event structure is read with a value not to exit. when you execute your exit event, LabVIEW finishes the loop iteration and starts a new one. Now the boolean can be read with a value to exit the loop, but the event structure waits for another event. Follow your code with highlight execution turned on to see what happens.
You have to set the boolean value inside the exit event and connect it through the event structure border to the conditional terminal.
11-15-2022 06:14 AM
Hi MaSta,
my blind guess would be that the stop terminal of the loop with event structure will receive the boolean variable "false" before you set it to "true". That would mean the loop with event structure goes to another iteration as is waiting/hanging - waiting for another UI action. As you press Exit for the second time, the loop will exit. Quick fix would be to simply ensure, that the loop stop terminal is read only after the event structure is executed.
If you attach your .vi, we'll be able to help you much easier.
Hope this helps,
11-15-2022 06:41 AM
@UliB wrote:
Hi,
you missed to attach your code so I have to assume what you did.
...but I described the situation. Sorry. The booleans aren't read outside of the event structure, because I use the events to trigger actions. The particular boolean control would then be read in the event and set a boolean variable which I wired to both loop exits. For me, the logic is OK and should work. I put probes on the exit wires. Both become TRUE when pressing the Exit menu item, but the loops don't exit.
11-15-2022 06:46 AM - edited 11-15-2022 06:50 AM
@Ajskrim: thanks, but I don't want to post my code here, as it's not public domain. The variable I set in the event remains TRUE when leaving the even structure, so the loop should exit.
Anyway, I solved it in a different way. In my first idea, I wanted to exit the loop and in a next step exit the application. Now I directly exit the application from within the even structure.
11-15-2022 07:01 AM
kb article which could be helpful:
and same question:
11-15-2022 10:07 AM - edited 11-15-2022 10:09 AM
@MaSta wrote:
Anyway, I solved it in a different way. In my first idea, I wanted to exit the loop and in a next step exit the application. Now I directly exit the application from within the even structure.
Directly exiting the application from within the event structure? How do you do that? Sounds potentially dangerous.
In your post you said that not only second press of Exit, but any triggered event will exit the loop. That just makes me more convinced it's what I said in my post above - you write "true" but for that iteration, the loop terminal already condition was already evaluated. That's why the next iteration - the event trigger you mention will exit the application.
In the snippet you can see the Stop button directly exits the while loop after exiting event structure. the FGV simulates your way of sharing the data between the loops since i don't know how it's done in your case. Notice that the red rectangle is the place where my value has to be TRUE if I want to exit the loop and that the condition needs to be evaluated only after the event loop is executed.
In the second snippet, I have my while loop condition in parallel with event structure and when I press Stop button, my Loop1 will not finish. I need to trigger the even one more time for the value to be propagated. That exactly describes your situation - and if you probe this - you'll see, that after you press stop, you'r loop1 will be hanging even though the "variable" is already in TRUE state.
attaching code and FGV.
Hope this helps,
11-15-2022 10:21 AM
That sounds like the definition of a race condition.
11-15-2022 10:22 AM
@Ajskrim wrote:
@MaSta wrote:
Anyway, I solved it in a different way. In my first idea, I wanted to exit the loop and in a next step exit the application. Now I directly exit the application from within the even structure.
Directly exiting the application from within the event structure? How do you do that? Sounds potentially dangerous.
In your post you said that not only second press of Exit, but any triggered event will exit the loop.
How? Stop/Exit VI.
No, I didn't say that. I clearly said that the Exit action from the menu triggers an event which sets a variable that is supposed to exit the loop. See the picture. The loops are supposed to exit whenever the loop iteration reaches the exit point, because the variable remains TRUE.
11-15-2022 10:47 AM
Stop/Exit VI - generally not recommended.
I understood that part about supposedly exiting the loop. What I am referring is the explanation why your loop1 exits only after you press Exit button for the second time.
Your picture shows that the suspicions UliB and me had were correct.
The
in loop1 will be executed right after you start your iteration 0 - before you even enter the Event structure. Thus, the evaluation for that iteration will be done Exit?=False.
Then you trigger Exit Case, which sets the Exit? indicator to True. So yes, the value of local variable will be also True. However - the condition for the iteration = 0 was already evaluated with Exit?=False. The loop will not finish, but go to iteration = 1. There, again right away - loop will evaluate the condition and finds out that yes local variable Exit?=True. However the Event structure is blocking the execution - that is why only after you trigger event again, you'll exit the loop.
You asked in the beginning how LV works at that point and this is how it does.