01-30-2021 03:59 PM
If i shouldn't use event structures, timed loops or for/while loops to do DB communication, what do you suggest? The database communication is currently made with a state machine, i'm just sending instructions to it with event structures (in this case).
01-30-2021 06:55 PM
You can do those things inside of loops. What you don't want to do is jam everything into your time critical loops. Also, code within an event structure should never take more than 10s of milliseconds to execute. If you have long running code in your event structe than it becomes unresponsive. Specifically this if for event structures that are handling UI stuff. If the event structure is purely handling user events generated by your code, it may be OK if the action within an event case takes a little longer to process.
What everyone is proposing is that you limit your time critical loops to only handle the time critical aspects of your application. If you have to log data in the DB, your timed loop should post that data to a queue or notifier or some other type of interprocess communication so that a separate task with get that data and act on it. This way your time critical code can operate at the spped/frequency it must while slower tasks like logging to a file or a DB do not impact your performance. those operations are handled in DIFFERENT loops/state machines.
02-01-2021 06:20 AM
Thank you all for your help.
It was indeed the while loop searching for the FracID = 8 that was the problem. When i changed something in the event loop, it affected the ProtocolDetails variable in such a way, that if ProtocolDetails didn't update before the loop started comparing, all the FracID values were 0 and thus the loop went to infinity, stalling the program.
I want to thank you again for all the great tips you've given me. I'll try to do better in the future 🙂
02-01-2021 11:12 AM
@AeroSoul wrote:
It was indeed the while loop searching for the FracID = 8 that was the problem. When i changed something in the event loop, it affected the ProtocolDetails variable in such a way, that if ProtocolDetails didn't update before the loop started comparing, all the FracID values were 0 and thus the loop went to infinity, stalling the program.
I want to thank you again for all the great tips you've given me. I'll try to do better in the future 🙂
Your loop will also never stop if the input array is empty. Maybe that's what was the case. For an experienced programmer, that loop immediately raises a lot of red flags.
Fortunately, there are much simpler AND safer alternatives. For example if you would autoindex on a FOR loop with conditional terminal, the loop is guaranteed to terminate once you run out of array elements. The boolean will tell you if a match was found or not, so you can substitute a sentinel value (e.g. -1) if that's the case. (Of course if it is a plain array, i.e. elements are not clusters, a simple "search array" would do the same).
02-01-2021 06:21 PM
@AeroSoul wrote:
But it was this large before and it worked fine. Also event structure still works fine, but the timed loop fails, even if i wait for the event to end. Or if i don't even trigger an event when entering the VI.
Taped on my cubicle wall is a set of translations. One of them is “But it works.” = “I know it’s crap, but I don’t care.”
02-02-2021 01:31 AM - edited 02-02-2021 01:34 AM
The problem with that is that while FracID 8 signifies "END" fraction... it's not the last one, it goes up to 12 (previous coder hastily patched up lack of fractions when new protocols came out).
EDIT:
What i ended up doing was that same while loop with a condition to terminate after 13 loops (i guess i could do for loop, now that i think about it, i misread your code...) and moved the loop to event handling instead of having it in timed loop, so it only executed once when protocol changed.
@paul_cardinale
Maybe i should put up those translations as well... 😄