I would like to abort a running subVI (consisting of a parallel loop) programatically.
The abort-invoke-node leaves an error message (Vi not in a state compatible with operation) and I cannot add a conditional terminal to the loop because it is a parallel one.
Is there any (nice or not nice) way to force the subVI to stop?
Solved! Go to Solution.
Abort will force something to stop. But certainly isn't a nice way to do it.
You can use notifiers or even global or local variables if used carefully to pass information between parallel loops.
Hard to give much information since you hadn't provided some code for us to look at and all the information is just a few short sentences.
Usually you will want the subVI to be abortable "internally". Instead of forcing it to close, it should have some sort of messaging system to allow callers to ask it to abort.
For example, give it a queue reference with a Boolean type (it could be a string, enum, whatever). Generate the queue in the calling VI, then give the reference to the subVI. When the caller wants to close the subVI, send a True on the queue.
Inside the subVI, have a "Dequeue element" at the start of its loop with a timeout set to 0. Each time the loop starts, check if there's a message in the queue. If there is, the caller has asked for an abort, so the subVI can shut down gracefully. If there are no elements in the queue, it'll continue on with no delay (since the timeout is 0).
You can also do this with User Events if your subVI is based around an Event Structure- the method is basically exactly the same.
The benefit to this method is that you can also send the subVI other information, not just a Stop request. For example, if the subVI is a datalogger, you could send it new data to log, or if it's a user interface, a new piece of data to plot. For simple "shut down" messages a Boolean type will suffice, but once you get the idea down you can look into using strings or enums bundled with a variant to send a "Command"/"Payload" style message to your subVI's.
There are lots of ways to skin this cat
The sub-VI can get a ref to the top level VI and register for a "close" event and shutdown when that happens.
There are other ways but that is about as simple as it gets.
Sorry that I did not make clear it is a parallel FOR loop that I am talking about. The subVI is just as simple as that, but I cannot add a conditional terminal to the loop, so I need to find another way to interrupt the loop.
Nope it will leave a broken arrow
Only if you leave it unwired.
Wire the local that flas the stop to the terminal and it will prevent the loop from iterating instead of spinning and devouring CPU as it counts.